Ris
Ris

Reputation: 1164

Public variable access

I have a class called Global.cs:

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

Now in the Main class,

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 
    }
}

What am I missing?

Upvotes: 0

Views: 109

Answers (2)

UncleDave
UncleDave

Reputation: 7188

Shouldn't you always be getting/setting "Id" rather than "id". As "id" is private.

Upvotes: 2

AgentFire
AgentFire

Reputation: 9780

Well, your XX class instance is more than one time.

If you need to persist some user-retalive info, try storing it into the SessionState.

If you need to just have the static class with some static data, add the static keyword to both class and its members.

Upvotes: 0

Related Questions