SunQuest
 
           Code Bank
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingCode Bank

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old February 6th, 2004, 11:06 AM
Rich's Avatar
Rich Rich is offline
Administrator
Developer Shed Admin.
 
Join Date: Sep 2003
Location: Fort Lauderdale, FL
Posts: 151 Rich User rank is Sergeant (500 - 2000 Reputation Level)Rich User rank is Sergeant (500 - 2000 Reputation Level)Rich User rank is Sergeant (500 - 2000 Reputation Level)Rich User rank is Sergeant (500 - 2000 Reputation Level)Rich User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 11 h 37 sec
Reputation Power: 10
.NET Static VariablesBetter than Application!

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 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:

x = 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.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>



[bold]Global.asax[/bold]

<%@ Application Classname="MyGlobals" %>

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

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

</Script>

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingCode Bank > .NET Static VariablesBetter than Application!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway