【问题标题】:What is the mistake I am making in the below C# code? [duplicate]我在下面的 C# 代码中犯了什么错误? [复制]
【发布时间】:2015-03-23 06:40:06
【问题描述】:

我有一个类似的界面

interface IEmpDeptRepository : IEmployeeRepository,IDepartmentRepository
 {
        IEnumerable<EmpDept> GetAll();        
 }

interface IEmployeeRepository
{
        IEnumerable<Employee> GetAll();       
}

interface IDepartmentRepository
{
        IEnumerable<Department> GetAll();        
}

EmpDept 类是

 class EmpDept 
        {
            public List<Employee> Employees { get; set; }
            public List<Department> Departments { get; set; }    
        }

现在EmpDeptRepository类如下

 public class EmpDeptRepository:IEmpDeptRepository
    {
        private List<EmpDept> empDepts = new List<EmpDept>();

        public EmpDeptRepository()
        {

            Add(new EmpDept { Employees = IEmployeeRepository.GetAll(), Departments = IDepartmentRepository.GetAll() });            
        }

        public EmpDept Add(EmpDept empDept)
        {
            if (empDept == null)
            {
                throw new ArgumentNullException("Employee Department");
            }            
            empDepts.Add(empDept);
            return empDept;
        }


        public IEnumerable<EmpDept> GetAll()
        {
            return empDepts;
        }

        IEnumerable<Employee> IEmployeeRepository.GetAll()
        {
            IEmployeeRepository employeeRepository = new EmployeeRepository();
            return employeeRepository.GetAll();
        }

        IEnumerable<Department> IDepartmentRepository.GetAll()
        {
            IDepartmentRepository deptRepository = new DepartmentRepository();
            return deptRepository.GetAll();
        }
    }

我收到以下错误

Error   1   Inconsistent accessibility: parameter type 'Models.EmpDept' is less accessible than method 'Models.EmpDeptRepository.Add(Models.EmpDept)'
Error   2   Inconsistent accessibility: return type 'Models.EmpDept' is less accessible than method 'Models.EmpDeptRepository.Add(Models.EmpDept)'
Error   3   Inconsistent accessibility: return type 'System.Collections.Generic.IEnumerable<Models.EmpDept>' is less accessible than method 'Models.EmpDeptRepository.GetAll()'

我犯了什么错误?

【问题讨论】:

  • 是的,Priyanka 将 class EmpDept 更改为 public class EmpDept 。它将解决错误。

标签: c# oop


【解决方案1】:

公开所有课程:

public class EmpDept 

【讨论】:

  • 我所有的课程都是公开的......
  • class EmpDept => public class EmpDept
  • 感谢工作。现在另一个问题。对于行 Add(new EmpDept { Employees = IEmployeeRepository.GetAll(), Departments = IDepartmentRepository.GetAll() }); ,错误:错误 2 非静态字段、方法或属性“IEmployeeRepository.GetAll()”需要对象引用 错误 3 非静态字段、方法或属性“IDepartmentRepository.GetAll”需要对象引用()'
  • 如消息所示:您无法通过接口访问GetAll()。你需要一个实例。即this.GetAll() 或类似的东西。
  • 非常感谢您的帮助
【解决方案2】:

将 EmpDept 类设为公开,因为当您想使用公开方法访问它时,它也必须公开。

【讨论】:

    猜你喜欢
    • 2016-12-09
    • 2019-10-07
    • 1970-01-01
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    • 2023-01-13
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多