We know that the application variable is a global variable. The difference between it and the session variable is that the application variable is the same for any user, the session variable obtains different values based on different users.
The following is a reprinted article that uses static variables of the global class to replace the application:
========================================================== ========================================================== =
In traditional ASP programs, we always use application objects to save application-level variables. This method is relatively memory-consuming. You can see the number of applications that are explicitly restricted by the Space vendor. Now that we have. net, we can make full use of the advantages of static variables. On the one hand, it can be used as an application-level variable, and on the other hand, its access speed is faster than the application object.
In. net, most objects are classes, including global. asax is no exception. in order to use static variables to replace the Application variables, we first need to be global. asax assigns a class name. use the following methods:
<% @ Application classname = "myglobals" %>
Then, we define static variables in the script tag. Note the 'public 'and 'shared' keywords:
VB:
<Script language = "VB" runat = "server">
Public shared sgreeting as string = "Visit harrisonlogic.com! "
</SCRIPT>
C #
<Script language = "C #" runat = "server">
Public static string sgreeting = "Visit harrisonlogic.com! "
</SCRIPT>
Now we have created the 'sgreeting 'variable set up, which can be called directly through the class name and variable name on the. ASPX page:
X = myglobals. sgreeting
How is it convenient?
======================
The source program is as follows:
Global. asax
<% @ Application classname = "myglobals" %>
<Script language = "VB" runat = "server">
Public shared sgreeting as string = "Visit harrisonlogic.com! "
</SCRIPT>
Page1.aspx
<% @ Page Language = "VB" %>
<HTML>
<Head>
<Script language = 'vb 'runat = Server>
Private sub page_load (byval sender as system. Object, byval e as system. eventargs) handles mybase. Load
Label1.text = myglobals. sgreeting
End sub
</SCRIPT>
</Head>
<Body>
<Asp: Label runat = server id = label1> </ASP: Label>
</Body>
</Html>
========================================================== ========================================================