Peter F
Peter F

Reputation: 83

C# Conflicts with the declaration

I have written this Employee class using C# winforms where Employee is promoted if Employee.Experience >=5.

I am getting an error on the line:
Employee.PromoteEmployee(empList,employee=>employee.Experience>=5);

conflicts with the declaration Delegateusage01.Employee

I have read an article on MSDN and few others on Stackoverflow on this topic but I just can not find that error.

namespace DelegateUsage01
{
    public partial class Form1 : Form
    {
        static List<Employee> empList = new List<Employee>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            CreateNewEmployee(new Employee() { Id = 101, Experience = 5, Name = "Peter", Salary = 4000 });
            CreateNewEmployee(new Employee() { Id = 100, Experience = 5, Name = "Peter", Salary = 4000 });
        }

        private static void CreateNewEmployee(Employee emp)
        {
            int index = empList.FindIndex(Employee => Employee.Id == emp.Id);
            if (index != -1)
            {
                Console.WriteLine("Two id can not be the same");
            }
            else
            {
                empList.Add(emp);
            }
            Employee.PromoteEmployee(empList,employee=>employee.Experience>=5);
            //getting error on this line
        }
    }

    delegate bool IsPromotable(Employee empl );

    class Employee
    {
        private int _id;
        private int _salary;
        private string _name;
        private int _experience;

        public int Id
        {
            get { return _id; }
            set
            {
                if (value <= 0)
                {
                    throw new Exception("ID can not be null");
                }
                _id = value;
            }
        }

        public string Name
        {
            get { return _name; }
            set
            {
                if (value == String.Empty)
                {
                    throw new Exception("Name can not be empty");   
                }
                _name = value;
            }
        }

        public int Salary
        {
            get { return _salary; }
            set
            {
                if(value<=0)
                { throw  new Exception("Salary cannot be negative");}

                _salary = value;
            }
        }

        public int Experience
        {
            get { return _experience; }
            set
            {
                if (value < 0)
                {
                    throw new Exception("Experience can not be negative");
                }
                _experience = value;
            }
        }

        public static void PromoteEmployee(List<Employee> employeeList,IsPromotable IsEligibleToPromote)
        {
            foreach (var employee in employeeList)
            {
                if (IsEligibleToPromote(employee))
                {
                    Console.WriteLine("Employee: {0} with employee id: {1} is eligible for promotion", employee.Name, employee.Id);
                }
            }
        }    
    }
}

Upvotes: 0

Views: 965

Answers (3)

Developerzzz
Developerzzz

Reputation: 1126

Change your method signature from

PromoteEmployee(empList,employee=>employee.Experience>=5)

to the following

PromoteEmployee(empList,Func<Employee,bool>);

then you call

Employee.PromoteEmployee(empList,employee=>employee.Experience>=5);

Upvotes: 1

E-Bat
E-Bat

Reputation: 4892

The problem is in the line that you call FindIndex from List, you cannot use the name Employee there, use this instead :

  int index = empList.FindIndex(e => e.Id == emp.Id);

Upvotes: 2

nvoigt
nvoigt

Reputation: 77374

Drop the delegate alltogether, that's obsolete .NET 1.0 stuff.

Your function should take a Func<Employee,bool> instead. That should compile.

Upvotes: 0

Related Questions