2016-10-11 103 views
-2

我面临“公共静态无效PromoteEmployee(列表employeeList,IsPromotable IsEligibleToPromote)”中的“PromoteEmployee”编译问题。C#:似乎无法将我的头围绕编译错误

如果有人能给我一个关于如何去做这件事的提示,我将不胜感激。

编辑:

错误是: “可访问性不一致:参数类型 'Program.IsPromotable' 比方法更少可访问的 'Program.Employee.PromoteEmployee(列表,Program.IsPromotable)'

class Program 
{ 
    static void Main(string[] args) 
    { 
     List<Employee> empList = new List<Employee>(); 

     empList.Add(new Employee() 
     { 
      ID = 101, 
      Name = "Test1", 
      Salary = 5000, 
      Experience = 5 
     }); 

     empList.Add(new Employee() 
     { 
      ID = 101, 
      Name = "Test2", 
      Salary = 2000, 
      Experience = 1 
     }); 

     empList.Add(new Employee() 
     { 
      ID = 101, 
      Name = "Test3", 
      Salary = 4000, 
      Experience = 4 
     }); 

     IsPromotable isPromotable = new IsPromotable(Promote); 

     Employee.PromoteEmployee(empList, isPromotable); 
    } 

    public static bool Promote(Employee emp) 
    { 
     if (emp.Experience >= 5) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 

    delegate bool IsPromotable(Employee empl); 

    public class Employee 
    { 
     public int ID { get; set; } 
     public string Name { get; set; } 
     public int Salary { get; set; } 
     public int Experience { get; set; } 

     public static void PromoteEmployee(List<Employee> employeeList, IsPromotable IsEligibleToPromote) 
     { 
      foreach (Employee employee in employeeList) 
      { 

       if (IsEligibleToPromote(employee)) 
       { 
        Console.WriteLine(employee.Name + " promoted"); 
       } 
      } 
     } 
    } 
} 
+6

给我们一个提示,告诉我们编译器错误是什么意思。 – hatchet

+1

什么是错误?它发生了什么?请给我们一些继续。 – Abion47

+0

Is is this line IsPromotable isPromotable = new IsPromotable(Promote);'???代表是私人代表 –

回答

5

我得到编译程序的错误是:发生

(67:28) Inconsistent accessibility: parameter type 'Program.IsPromotable' is less accessible than method 'Program.Employee.PromoteEmployee(System.Collections.Generic.List<Program.Employee>, Program.IsPromotable)' 

此错误的原因是PromoteEmployeepublic,但IsPromotable是私人的。

如果从外部类节目有人想打电话PromoteEmployee,他不能这样做,因为他不能创造的Program.IsPromotable一个实例,因为它是私有的Program

- https://stackoverflow.com/users/424129/ed-plunkett

要修复它使IsPromotableinternalpublic让别人以外Program可以创建它。

或者,您可以使PromoteEmployee私密。

+0

@DStanley你是对的,谢谢 –

+2

要指出的是,如果外部程序*中的某个人*想要调用'PromoteEmployee',他不能这样做,因为他不能创建'Program.IsPromotable'的实例,因为它是私有的到'程序'。所以编译器警告你你正在做一些毫无意义的事情。 –

+0

谢谢@EdPlunkett很好的描述,在答案中引用。 –

1

这是一个工作dotnetfiddle。 https://dotnetfiddle.net/4iuQjt

using System; 
using System.Collections.Generic; 

public class Program 
{ 
    public static void Main(string[] args) 
    { 
     List<Employee> empList = new List<Employee>(); 

     empList.Add(new Employee() 
     { 
      ID = 101, 
      Name = "Test1", 
      Salary = 5000, 
      Experience = 5 
     }); 

     empList.Add(new Employee() 
     { 
      ID = 101, 
      Name = "Test2", 
      Salary = 2000, 
      Experience = 1 
     }); 

     empList.Add(new Employee() 
     { 
      ID = 101, 
      Name = "Test3", 
      Salary = 4000, 
      Experience = 4 
     }); 

     IsPromotable isPromotable = new IsPromotable(Promote); 

     Employee.PromoteEmployee(empList, isPromotable); 
    } 

    public static bool Promote(Employee emp) 
    { 
     if (emp.Experience >= 5) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 

    public delegate bool IsPromotable(Employee empl); 

    public class Employee 
    { 
     public int ID { get; set; } 
     public string Name { get; set; } 
     public int Salary { get; set; } 
     public int Experience { get; set; } 

     public static void PromoteEmployee(List<Employee> employeeList, IsPromotable IsEligibleToPromote) 
     { 
      foreach (Employee employee in employeeList) 
      { 

       if (IsEligibleToPromote(employee)) 
       { 
        Console.WriteLine(employee.Name + " promoted"); 
       } 
      } 
     } 
    } 
}