Vladimir Sukov Vladimir Sukov - 1 month ago 7
C# Question

C#: Can't seem to wrap my head around a compile error

I'm facing a compilation issue in "PromoteEmployee" within "public static void PromoteEmployee(List employeeList, IsPromotable IsEligibleToPromote)".

Would appreciate if someone could give me a hint on how I should go about this.

EDIT:

The error is:
"Inconsistent accessibility: parameter type 'Program.IsPromotable' is less accessible than method 'Program.Employee.PromoteEmployee(List, 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");
}
}
}
}
}

Answer

The error I get compiling your program is:

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

This error occurs because PromoteEmployee is public, but IsPromotable is private.

If somebody from outside class Program wants to call PromoteEmployee, he can't do that, because he can't create an instance of Program.IsPromotable, because it's private to Program.

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

To fix it make IsPromotable internal or public so that others outside Program can create it.

Alternatively you can make PromoteEmployee private.

Comments