Variable Scope in ASP

Domain Hosting image
Web Hosting
Dedicated server
ssl certificate
Web Design image
Email

The scope on routine, of a variable determines which script commands can access a variable. A variable declared inside a procedure has local scope; the variable is created and destroyed every time the procedure is executed. It cannot be accessed by , anything outside the procedure. A variable declared outside a procedure has global scope; its value is accessible and modifiable by any Script command or, an ASP page.

If you declare variables, a local variable and a global variable can have the, same name. Modifying the value of one will not modify the value of the other. If you do not declare variables, however, you might inadvertently modify the value of the global variable. For example, the following script commands return the value 1 even though there are two variables named Y.

<%
Dim Y
Y =1
Call Set Local Variable
Response. Write Y

Sub Set Local Variable
Dim Y
Y=2
End sub
%>

The following script commands, on the other hand, return the value 2 because the variables are not explicitly declared. when the procedure call sets Y to 2, the scripting engine assumes the procedure intended to modify the global variable.
<%
Y =1
Call Set Local Variable
Response. Write Y

Sub Set Local Variable
Y = 2
End Sub
%>

To avoid problems, develop the habit of explicitly declaring all variables. This is particularly important if you use the #include statement to include files into your ASP page. The included script is contained in a separate file but is treated as though it were part of the including file. It is easy to iorget that you must use different names for variables used in the main script and in the included script unless you declare the variables.

Giving Variables Session or Application Scope

Global Variables are accessible only on a single ASP page. To make a variable accessible beyond a single page, give the variable either session or application scope. Session scoped variables are available to all pages in one ASP application that are requested by one user. Application scoped variables are available to all pages in one ASP application that are requested by any user. Session variables are a good way to store information for a single user, such as preferences or the user's name or identification. Application variables are a good way to store information for all users of a particular application, such as an application specific greeting or initial values needed by the application.

ASP provides two built in objects into which you can store variables: the Session object and the Application object.

You can also create object instances with session or application scope.



Domain Name Search

www.


Copyright (C) 2007. Web Domain design hosting. All rights reserved.