C# - 新创建的自定义类变量不存在

时间:2016-07-20 09:41:22

标签: c# class

我创建了一个名为DashObject的新类,然后立即创建了一个DashObject示例。但是当我尝试访问它时,IDE说它不存在。

我尝试使用此类管理Amazon Dash设备,但由于某种原因我甚至无法访问它们。

    class DashObject
    {
        // Definitions
        string DashName; 
        string DashIdentifier;
        int DashFunction;
        string DashFunctionInfo;
        // Constructor
        public DashObject(string dashName, string dashIdentifier, int dashFunction, string dashFunctionInfo)
        {
            DashName = dashName;
            DashIdentifier = dashIdentifier;
            DashFunction = dashFunction;
            DashFunctionInfo = dashFunctionInfo;
        }
    }

    //Create example DashObject
    DashObject example = new DashObject("Example", "44650DFD85E9", 1, "prsl");

    //Attempt to access example (this line causes build to fail)
    MessageBox.Show(example.DashName);

2 个答案:

答案 0 :(得分:1)

确保您有publicinternal访问修饰符。将您的代码更改为:

public class DashObject
{
    // Definitions
    public string DashName; 
    public string DashIdentifier;
    public int DashFunction;
    public string DashFunctionInfo;
    // Constructor
    public DashObject(string dashName, string dashIdentifier, int dashFunction, string dashFunctionInfo)
    {
        DashName = dashName;
        DashIdentifier = dashIdentifier;
        DashFunction = dashFunction;
        DashFunctionInfo = dashFunctionInfo;
    }
}

答案 1 :(得分:0)

要访问该字段,您必须将其设置为publicinternal

public string DashName; 

internal string DashName;