Static Variable hell in ASP.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tskrish
    New Member
    • Aug 2008
    • 9

    Static Variable hell in ASP.NET

    Hi,

    In any ASP.NET application the life of the static variable sounds peculiar.
    Consider a scenario like this,

    public static int i = 0; //Static declaration

    protected void Page_Load(objec t sender, EventArgs e)
    {
    if (i == 0)
    {
    Response.Write( "First time: i=" + i.ToString() );
    }
    else
    {
    Response.Write( "Else Part: i=" + i.ToString());
    }

    i++; //Increment the variable count by 1.
    }

    Now comes the game. Run the application....

    First time it shows as: "First time: i=0"
    Now stop the application (NOT the localhost), re-run the application

    Now it shows output as: "Else Part: i=1" (again stop the application re-run) the 'i' value will be keep on incrementing only.

    Can anybody tell me how to avoid this kind of behaviour (please don' tell avoid using static variables in web, already I have googled a lot and much of them tell the same thing, not to use static variable in web app, as if like an intellegent reply.)

    Forget about above scenario.

    Take this. If some xyz person has developed a class library project with lot of use of static variables (say business component). Since it is a class library project it is INDEPENDENT of where it is going to get plugged in (it can be windows app or web app it doesn't matter to xyz).

    Now, if in your web application you are plugging in this DLL then what will happen to the consuming web application? I believe the 1st static scenario hell will occur here (and it is happing also).

    So is there a wise solution for the above scenarios to avoid this kind of static hell???

    with regards,
    Krish TS
  • ThatThatGuy
    Recognized Expert Contributor
    • Jul 2009
    • 453

    #2
    an alternate solution for static variables is Application variables... which you declare in global.asax file....

    Now considering your scenario you... should not use static variables...

    As values of a static variable remain static as long as your application is running....
    in windows application.... the value persists as long as the application is running... so is in web applications... ..

    In a web application your applications is always running unless you close the localhost server.... then only ny static or application variables is set to it's default value....

    Whereas using Session variables will solve your problem .... as Session variables persists as long as the browser is running.....

    Comment

    • tskrish
      New Member
      • Aug 2008
      • 9

      #3
      Thanks ThatThatGuy,

      But what is the solution for this scenario,

      If some xyz person has developed a class library project with lot of use of static variables (say business component). Since it is a class library project it is INDEPENDENT of where it is going to get plugged in (it can be windows app or web app it doesn't matter to xyz). Say this DLL is developed by you Onsite Architect who is least bothered about Win or Web, because obviously that person is a component developer only and he/she is not going care about this (logically sounds true also).

      Now tell me, how (y)our web application (consumer) going to handle this DLL.

      This scenario is very much possible in most of cases, and happing also.

      Let me know.

      Comment

      Working...