简体   繁体   English

由于其保护级别,成员变量不可访问

[英]Member variable inaccessible due to its protection level

I am trying to access a variable in my implementation and it states that "its inaccessible due to its protection level"我正在尝试访问我的实现中的一个变量,它指出“由于其保护级别而无法访问”

namespace mytest
{
    public class IDatabaseSettings
    {
        string connection { get; set; }
    }
}

namespace mytest
{
    public class DatabaseSettings : IDatabaseSettings
    {
        public string connection{ get; set; }
    }
}

IDatabaseSettings settings = new DatabaseSettings();
settings.connection <--- is inaccessible due to its protection level

Any suggestions on why this might be happening ?关于为什么会发生这种情况的任何建议?

While you have a public class, the property you created is private.虽然您有一个公共类,但您创建的属性是私有的。 The default when you don't specify an access modifier on a class is private.未在上指定访问修饰符时的默认值是私有的。 Interfaces do not allow access modifiers as everything is assumed public (otherwise the interface would not be useful).接口不允许访问修饰符,因为一切都假定为公共(否则接口将没有用)。

Use this instead on your DatabaseSettings class (the interface is fine):在您的DatabaseSettings类上使用它(界面很好):

public string connection { get; set; }

Also, your interface is defined as a class.此外,您的接口被定义为一个类。 Define it as an interface instead:将其定义为接口:

public interface IDatabaseSettings
{
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM