Last post Jan 05, 2011 11:33 AM by adrenterprises
Dec 02, 2009 05:13 PM|InterOrion|LINK
I need to declare and set a variable in my application that will be used for an external class library?
I was thinking that maybe it could be done in the web.config?
Basically - rather than needing to include it in EVERY page of my application, I was hoping I could declare the Class Library globally in the application, as well as the settings for it?
I hope this makes sense.
Dec 03, 2009 10:57 AM|LudovicoVan|LINK
Your requirement is not too clear (what's the lifecycle of your external lib, and how are these variables related?), anyway a straightforward way to global variables at web site level is to define some Application variables in the Global.asax, at Application_Start.
(That is actually quite nice because, just as for the Web.config, if you edit the Global.asax the application restarts. In fact, you could define basic values in the Web.config, then have the Global read those values and initialise some application variables
with strong typed values, or maybe by reading from a database, etc. etc.)
Dec 03, 2009 11:41 AM|atconway|LINK
An Application variable will work as suggested earlier, but just be careful of the scope of the Application variables. They will hold value for
all applications in scope, not just the current instance. This may actually be the desired effect, as it sounds like it may be based on your brief description. Maybe an example of this value could be "MyCompanyName". In that sense this value is
static, and will apply to all application instances regardless.
From the MSDN on Application Variables:
"Data that is shared by multiple sessions and does not change often is the ideal type of data to insert into application-state variables. Because application state is accessible to all pages in an application, storing information in application state
can mean keeping only a single copy of the information (for instance, as opposed to keeping copies of information in session state or in individual pages)."
Now if this is not the case, you may need to step it down a level to a 'Session' varibale. It too is declared in the Global.asax file (in the Session_Start method), but its scope is within the current application session and will be user specefic. Lets'
say for example the 'Global' value you are mentioning for that external class is username, like "jSmith". In this case this would not be a good candidate for an Application variable, but more appropriate for a Session variable since it is user specefic.
From the MSDN on session variables:
"You can store your session-specific values and objects in session state, which is then managed by the server and available to the browser or client device. The ideal data to store in session-state variables is short-lived, sensitive data that is specific
to an individual session."
Reading through the following article will give you a better understanding:
ASP.NET State Management Recommendations:
Lastly, if the value is static and the same for all applications, then you could store it as an 'AppSetting' in the web.config and then expose it via a class property, session variable, application variable, or directly to pass along to your library.
Dec 03, 2009 11:53 AM|LudovicoVan|LINK
> An Application variable will work as suggested earlier, but just be careful of the scope of the Application variables.
How wise, glad to have your attention... :P
Dec 06, 2009 08:44 AM|InterOrion|LINK
Ideally it would be great to be able variables globally. Since I don't make use of Global.asa ever, I completely forget that it exists!
If you do declare an Application Variable, how can you change it from with the application, or via referenced Class Library?
Jan 05, 2011 11:33 AM|adrenterprises|LINK
You can create a class called "SoftwareSettings.cs" and reference that in your pages to read and write global variables for use in your application. It does require the use of one other class that actually does the reading and writing. It is pretty simple
to use. For information on setting these classes up visit:
Once you have setup the classes, you can simply access your variables by doing this:
SoftwareSettings settings = new SoftwareSettings();
//--- read a variable
string myVar = settings.VariableName;
//--- set a variable
settings.VariableName = "value";