繁体   English   中英

C#与声明冲突

[英]C# Conflicts with the declaration

我已经写此Employee使用其中C#的WinForms类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);
                }
            }
        }    
    }
}

问题出在您从List调用FindIndex的行中,您不能在此处使用名称Employee,而应使用以下名称:

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

从更改方法签名

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

到以下

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

然后你打电话

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

一起删除代理,这是.NET 1.0的过时内容。

您的函数应改用Func<Employee,bool> 那应该编译。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM