Inconsistent accessibility problem [duplicate]

主宰稳场 提交于 2019-11-28 08:53:22

问题


I am following Rob Conery MVC Storefront tutorial series and I get an Inconsistent accessibility error from the following constructor public SqlCatalogRepository(DB dataContext) :

public class SqlCatalogRepository : ICatalogRepository
{
    DB db;

    public SqlCatalogRepository()
    {
        db = new DB();
        //turn off change tracking
        db.ObjectTrackingEnabled = false;
    }


    public SqlCatalogRepository(DB dataContext)
    {
        //override the current context
        //with the one passed in
        db = dataContext;
    }

Here is the error message : Error 1 Inconsistent accessibility: parameter type 'SqlRepository.DB' is less accessible than method 'Data.SqlCatalogRepository.SqlCatalogRepository(SqlRepository.DB)'


回答1:


Your DB class is not public, so you can't make a public method (or constructor) that takes it as a parameter. (What would callers outside your assembly do?)

You need to either make the DB class public or make the SqlCatalogRepository class (or its constructor) internal.

Which one you do will depend where your types are being used.
If the SqlCatalogRepository is only meant to be used inside your assembly, you should make it internal. (internal means that it's only visible to other types in the same assembly)

If it's meant to be exposed by your assembly to other assemblies, you should make the class public but the constructor internal.

If the DB class itself is meant to be used by types outside your assembly, you should make the DB class itself public.




回答2:


The type DB is used in a public constructor of a public type. Therefore, the type DB must itself be public.




回答3:


Check the accessor on the DB class (you dont show it here) it would need to be a Public class in oreder to allow it to be passed in to the overloaded constructor.



来源:https://stackoverflow.com/questions/2139740/inconsistent-accessibility-problem

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!