2016-10-11 102 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"); 
       } 
      } 
     } 
    } 
}