C#与声明冲突

时间:2014-02-15 12:53:37

标签: c# winforms

我使用C#winforms编写了此Employee课程,Employee提升Employee.Experience >=5

我收到错误:
Employee.PromoteEmployee(empList,employee=>employee.Experience>=5);

  

与声明Delegateusage01.Employee

冲突

我已经在MSDN上阅读了一篇关于此主题的关于Stackoverflow的文章,但我发现这个错误。

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);
                }
            }
        }    
    }
}

3 个答案:

答案 0 :(得分:2)

问题出在你从List调用FindIndex的行中,你不能在那里使用名称Employee,改为使用它:

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

答案 1 :(得分:1)

更改方法签名
PromoteEmployee(empList,employee=>employee.Experience>=5)

以下

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

然后你打电话

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

答案 2 :(得分:0)

完全删除委托,这是过时的.NET 1.0内容。

您的功能应改为Func<Employee,bool>。那应该编译。