Use static variables in. NET instead of application variables

Source: Internet
Author: User
Tags variables script tag
application| variable | static wincheer|2002-3-1
--------------------------------------------------------------------------------


In traditional ASP programs, we always use the Application object to hold the application-level variables. This is a much more memory-intensive approach----look at the amount of space vendors blatantly restricting application. Well, now that we have. NET, we can give full play to the advantages of static variables. On the one hand, you can be an application-level variable, and on the other hand, he accesses faster than the Application object.

In. NET, most objects are classes, including Global.asax. In order to use static variables instead of application variables, we first need to give the Global.asax a class name. The following ways:

<%@ application Classname= "Myglobals"%>

Then we define the static variables in the script tag, paying attention to 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 established the ' sgreeting ' variable set up, which can be called directly through the class name and variable name in the. aspx page:

x = myglobals.sgreeting

How is it, isn't 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>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.