0

我有一堂课叫Global.cs

public class Global
{
    private string id= string.Empty;
    public string Id
    {
        get { return id;}
        set { id= value; }
    }
}

现在在Main课堂上,

public class Main
{
    public Global objGlobal;
    protected void Page_Load(object sender, EventArgs e)
    {
        objGlobal= new Global();
        objGlobal.id="XX001";
    }
    public void Setdata()
    {
        // Trying to access objGlobal.id value here but it's null 
    }
}

我错过了什么?

4

2 回答 2

2

您不应该总是获取/设置“Id”而不是“id”。因为“id”是私有的。

于 2013-02-15T19:43:00.453 回答
0

好吧,您的XX类实例不止一次。

如果您需要保留一些用户重复信息,请尝试将其存储到SessionState中。

如果您只需要包含一些静态数据的静态类,请将static关键字添加到类及其成员中。

于 2013-02-15T19:41:54.810 回答