Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to define global variables?
Message
 
To
09/11/2003 18:53:31
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
00848129
Message ID:
00848345
Views:
12
This message has been marked as a message which has helped to the initial question of the thread.
>This has started as what seemed a simple task. But, then I did some searches and found that this is complicated. Can anyone point me to the proper way of defining a global variable in an ASP.NET application?

You can define static (shared) variables on the Global object (global.asax) and they are visible throughout the Web application. In fact you can create a custom class and define statics on it as well and they will also be globally accessible (assuming the namespace is visible).

I do this all the time. I create an App class which underneath it has static sub objects such as Configuration, State etc. which are then accessible as:

App.Configuration.ConnectionString

Note however that you must lock access to these properties as they are not thread safe. So you might need to use:

lock(App.Configuration)
{
App.Configuration.ConnectionString = "somenew value";
}

if there is a chance that the value is updated from multiple locations.

I prefer this mechanism over the others because it gives you a stronly typed reference to the property which means you get Intellisense, compiler safety and to read at least you can simply retrieve the value wihtout having to check for nulls and un-boxing from Object type.


Other than that you can use either the Cache object or the Application object to access variables throughout the application.

+++ Rick ---
+++ Rick ---

West Wind Technologies
Maui, Hawaii

west-wind.com/
West Wind Message Board
Rick's Web Log
Markdown Monster
---
Making waves on the Web

Where do you want to surf today?
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform