In an ASP.NET page, the Application object represents the web application in which the page belongs.
.NET provides a class called HttpApplication, which is part of the System.Web namespace. This class represents the Application object in ASP.NET. An instance of this class is already created for you and this is called "Application". You can access various methods and properties of the class HttpApplication using the object "Application".
Just like you can store values in session variables, you can store key/value pairs in Application variables too. The scope of an Application variable is the entire web application, while the scope of a session variable is only a particular session.
For example, if you assign a value into an Application variable as shown below, all users of the web site can access this value.
Application("Current Stock Price") = 145.25
You can store values that do not change often and store them in the application variable. For example, your web site displays the current stock price of your company in the top of all pages of your web site. This stock price is not specific for any users. You may need to access other government agencies database to retrieve this value.
There is no need to contact an external agency and get this value for all pages displayed in the site. You can retrieve it once and save into an Application variable.
Once the value is retrieved and stored in Application variable, you can get it from this variable and display in all the pages. It will be several times faster to retrieve value from application variable (which is in the memory) than accessing an external agency.
Example - storing to application variable:
Application("Current Stock Price") = GetFromExternalAgency()
Example - retrieving from application variable:
lblStockPrice.Text = "Stock Price: " & Application("Current Stock Price")
Scope of an Application Variable
An application variable is valid till the application shuts down or the aspnet process is terminated. In a web server, this process may run for several days without shutting down.
In the above example, if stock price is retrieved and stored in the application variable, it will remain there until the process is restarted. The stock prices may change during this time, but you will not get the updated price.
In such cases, you must have some mechanism to retrieve the stock price from external agency at some regular intervals and store into the application variable so that you will see the updated price. One way to do this is, store the retrieval time also into another application variable and check often how long it has been after it was last retrieved. If the elapsed time is more than 1 hour or so, retrieve again.
ASP.NET provides another class called "Cache" which can be used to store key/value pairs like Application variables. This class is more efficient than using Application variables. Also, it provides several additional methods which will allow you to automatically expire the value in cache after some interval of time or when some "specific" event happens.
You will learn more about the Cache class in upcoming chapters.