C#:似乎无法解决编译错误

时间:2016-10-11 19:38:13

标签: c#

我在" PromoteEmployee"中遇到了编译问题。在" public static void PromoteEmployee(List employeeList,IsPromotable IsEligibleToPromote)"。

如果有人能给我一个关于我应该怎么做的提示,我将不胜感激。

编辑:

错误是: "可访问性不一致:参数类型' Program.IsPromotable'比方法' 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");
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分: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

要修复它,请IsPromotable internalpublic以便Program之外的其他人可以创建它。

或者,您可以将PromoteEmployee设为私有。

答案 1 :(得分: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");
                }
            }
        }
    }
}