1
Reply

Static Varible Sharing Problem

Shiva Nandan

Shiva Nandan

Jun 10 2009 3:02 AM
2.9k
Hai Friends

I have declared a variable outside all the function in an aspx.cs page, like

 public partial class StaticVar : System.Web.UI.Page
 {

    static int a;

    protected void Page_Load(object sender, EventArgs e)
    {

        Response.Write(a.ToString());
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        a = 50;
        Response.Write("set 50, a= " + a.ToString());
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        a = 60;
        Response.Write("set 60, a= " + a.ToString());
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Response.Write("a= " + a.ToString());
    }
}


The problem is that when more than one user access this page and modifies the variable a, all the users gets the new value for a,
   i.e., they loose the value what they had set for a. They get the value that is been set by the last user.

If I remove 'Static' from its declaration, then its value is not available to other functions.

How to solve this problem.
Please help me.

Answers (1)