Plip's Weblog

Phil Winstanley - British .NET chap based in Lancashire. Enjoys tea and tech. Working for Microsoft.

ASP.NET Application variables or Static Members?

I was not aware that it was possible to use Static members in Classes and have them retain their state until today when myself and a few colleagues were discussing some code in an older application Portfolio Europe developed called EBCentre which is a stock management tool for Used Car Dealerships used throughout the UK and Europe.

After a little searching one of my colleagues came up with an MSDN article that states: -

  • A key reason that the Application object exists in ASP.NET is for compatibility with classic ASP code—to allow easier migration of existing applications to ASP.NET. If you are creating an ASP.NET application from scratch, you will want to consider storing data in static members of the application class rather than in the Application object. This will yield a performance increase over using the Application object. An explanation of using statics, as well as some sample code, is listed in Microsoft Knowledge Base article Q312607: INFO: Application Instances, Application Events, and Application State in ASP.NET.
  • That's something new I learnt for today :)

    Posted: Dec 01 2003, 07:03 PM by Plip | with 9 comment(s)
    Filed under: ,

    Comments

    Nickolas Kanyo said:

    That was new too me as well, cheers for pointing out!
    # January 30, 2004 1:04 AM

    smitha said:

    This is a tough part in .net .please explain with examples

    # December 23, 2007 9:18 AM

    Vlad said:

    I agree, could you please throw some examples in?

    # November 6, 2008 1:13 PM

    Thomas Williams said:

    Sample code to show the difference between using Application storage and static declarations.

    using System.Web;

    using System.Web.Services;

    public class MyAppClass

    {

    /// <summary>

    /// I can store my application variable for later use.

    /// </summary>

    private HttpApplicationState app;

    /// <summary>

    /// From your webapplication, use the public System.Web.Services.Application property.

    /// </summary>

    /// <param name="state"></param>

    MyAppClass(HttpApplicationState app)

    {

    this.app = app;

    }

    /// <summary>

    /// Get and Set using the application state property.

    /// </summary>

    public string MyApplicationProperty

    {

    get

    {

    try

    {

    app.Lock();

    return (string)app["MyAppInstance"];

    }

    finally

    {

    app.UnLock();

    }

    }

    set

    {

    try

    {

    app.Lock();

    app["MyAppInstance"] = value;

    }

    finally

    {

    app.UnLock();

    }

    }

    }

    }

    // Note: This class got no constructor

    public class MyStaticClass

    {

    static string myGlobalVar;

    static object applock = new object();

    /// <summary>

    /// Get and Set using the application state property.

    /// </summary>

    public string MyApplicationProperty

    {

    get

    {

    lock (applock)

    {

    return myGlobalVar;

    }

    }

    set

    {

    lock (applock)

    {

    myGlobalVar = value;

    }

    }

    }

    }

    # June 2, 2009 4:53 PM

    Thomas Williams said:

    correction:

    Forgot to declare the 'MyStaticClass.MyApplicationProperty' property as static:

    public string MyApplicationProperty

    should be

    public static string MyApplicationProperty

    # June 2, 2009 4:58 PM

    Code Capers said:

    ASP.NET - Creating a Custom Tag Cloud

    # February 11, 2010 1:33 PM

    BitStranger said:

    Great ! Never tought I could have used a static class this way !!!! You made my day ...

    # February 20, 2010 5:36 PM

    Xaqron said:

    Thanks, very useful

    # December 30, 2010 11:32 AM

    Ivan said:

    Careful though, static variables are not thread safe. You might want to make the setter private.

    # October 30, 2011 2:08 AM