In traditional ASP, we always had the Application object to store application-wide variables in. This of course came at the price of memory allocations. In .NET we can now take advantage of Static Variables, which in most cases can be faster than accessing the Application object.In .NET, most objects are actually classes, and Global.asax is no exception. To take advantage of this, we first have to give our Global.asax a Classname. We do this by adding the directive naming mine 'MyGlobals':<%@ Application Classname="MyGlobals" %>Then, we specify our Static Variable inside the script tags, using the 'Public' and 'Shared' keywords in the Global.asax: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 that we have the variable 'sGreeting' set up, we can call it directly from our .aspx page using the Class name and the Variable name:x = MyGlobals.sGreetingGive it a shot!
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: