Fairly interesting ASP.NET forums question - on using Global static variables instead of Application state in ASP.NET (English)

Fairly interesting ASP.NET forums question - on using Global static variables instead of Application state in ASP.NET

Monday, 05 July 2004

//

Less than a minute

I was replying to this forum post I actually couldn't find a good example on how to do this - wierd, it's such a common thing to do. So, the problem is, is there a better method than using the Application object to store objects which should be available for the entire lifetime of the application? Yes, global static variables are a great way to do this. Simply, you use the global.asax.cs file to define a static property - you can then access this simply using the Global.* in your code. For example, in the global.asax.cs:

public static ArrayList TestArrayList = new ArrayList(); Now, in any class I can access this...like so...
if(Global.TestArrayList != null)

{

for(int i=0; i <1000; i++)

{

Global.TestArrayList.Add(i);

}

} Cool, no copy required, (yes I do realise I should actually be locking this first to ensure thread safety...)So, short one, but might come in handy for someone! Oh, I did a little example app which also shows using this in an HttpModule, you can download it here

logo

© 2025 Scott Galloway — Unlicense — All content and source code on this site is free to use, copy, modify, and sell.