![]() |
Variable Scope in ASP![]() ![]() ![]() ![]() ![]()
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.
<% Sub Set Local Variable 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. Sub Set Local Variable 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 NamesASP IDC and HTX Files Creating ASP Pages Adding Script Commands Mixing HTML and Script Commands ASP Directives Setting the Primary Scripting Language Using VBScript and JScript on a Server Variable Scope Session Scope Application Scope
Defining Procedures Calling Procedures Components ASP ApplicationsWeb DesignWeb HostingE Commerce |
| Home | Web Hosting | Web Design | Sitemap |
| Copyright (C) 2007. Web Domain design hosting. All rights reserved. |