.NET Static VariablesBetter than Application!

[introtext]In traditional ASP we always had the Application object to store application-wide variables in. This of course cam 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.[/introtext]

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 6
June 01, 2003
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement
In traditional ASP we always had the Application object to store application-wide variables in. This of course cam 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 Classname and the Variable name:


MyGlobals.sGreeting

Give it a shot! The source code is below:[bold]Page1.aspx[/bold]


<% @Page Language="VB" %>
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<
HTML>
    <
HEAD>
        <
script Language='vb' runat=server>
                Private 
Sub Page_Load(ByVal sender As System.ObjectByVal e As System.EventArgsHandles MyBase.Load
                    Label1
.Text MyGlobals.sGreeting
                End Sub
        </script>
    </HEAD>
    <body>
            <asp:label runat=server id=Label1></asp:label>
    </body>
</HTML>

[bold]Global.asax[/bold]


<%@ Application Classname="MyGlobals" %>

<
Script language="vb" runat="server">

    Public 
Shared sGreeting as String "Visit HarrisonLogic.com!"

</Script>

- Chris Harrison
blog comments powered by Disqus
ASP.NET CODE ARTICLES

- How to Use the ListBox Control in ASP.NET 2.0
- How to Load XML Documents in ASP.NET 2.0
- DataGrid Code
- ASP.NET Guestbook
- User Controls and Client Side Scripting
- ASP.NET Programming with Microsoft's AS...
- ASP.NET Basics (part 3): Hard Choices
- ASP.NET Basics (part 2): Not My Type
- ASP.NET Basics (part 1): Nothing But .Net
- Directory Tree Browser
- How to get the confirmation of Yes/No from a...
- Complete example using custom errors and wri...
- Paging Certain # records per page .NET style
- General Methods of formatting and Subtractin...
- .NET LinkButton web control

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials