Using Variables and Constants

A variable is a named storage location in the computer's memory that contains data, such as a number or a text string. The data contained in a variable is called the variable's value. Variables give you a way to store, retrieve, and manipulate values using names that help you understand what the script does.

Declaring and Naming Variables

Follow the rules and guidelines of your scripting language for naming and declaring variables. Even if you are not required to declare a variable before using it, it is a good habit to develop because it helps prevent errors. Declaring a variable means to tell the script engine that a variable with a particular name exists so that you can use references to the variable throughout a script.

VBScript

VBScript does not require variable declarations, but it is good scripting practice to declare all variables before using them. To declare a variable in VBScript, use the Dim, Public, or Private statement. For example:

<% Dim UserName %>

You can use the VBScript Option Explicit statement in an .asp file to require variables to be explicitly declared. The Option Explicit statement must appear after any ASP directives and before any HTML text or script commands. This statement only affects ASP commands that are written in VBScript; it has no affect on JScript commands.

JScript

Microsoft JScript requires variable declarations only for variables that are local to a procedure, but it is good scripting practice to declare all variables before using them. To declare a variable, use the var statement. For example:

<% var UserName; %>

Variable Scope

The scope, or lifetime, 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 on 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 a 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 SetLocalVariable
Response.Write Y

Sub SetLocalVariable
  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 SetLocalVariable
Response.Write Y

Sub SetLocalVariable
  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 forget 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. For more information, see Setting Object Scope.

Session Scope

To give a variable session scope, store it in the Session object by assigning a value to a named entry in the object. For example, the following commands store two new variables in the Session object:

<% 
Session("FirstName") = "Jeff"
Session("LastName") = "Smith" 
%>

To retrieve information from the Session object, access the named entry by using the output directive (<%=) or Response.Write. The following example uses the output directive to display the current value of Session("FirstName"):

Welcome <%= Session("FirstName") %>

You can store user preferences in the Session object, and then access those preferences to determine what page to return to the user. For example, you can allow a user to specify a text-only version of your content in the first page of the application and apply this choice on all subsequent pages that the user visits in this application.

<% If Session("ScreenResolution") = "Low" Then %> 
This is the text version of the page.
<% Else %> 
This is the multimedia version of the page.
<% End If %> 

Application Scope

To give a variable application scope, store it in the Application object by assigning a value to a named entry in the object. For example, the following command stores an application-specific greeting in the Application object:

<% Application("Greeting") = "Welcome to Exploration Air" %>

To retrieve information from the Application object, use the ASP output directive output directive (<%=) or Response.Write to access the named entry from any subsequent page in the application. The following example uses the output directive to display the value of Application("Greeting"):

<%= Application("Greeting") %>

Using Constants

A constant is a name that takes the place of a number or string. Some of the base components provided with ASP, such as ActiveX Data Objects (ADO), define constants that you can use in your scripts. A component can declare constants in a component type library, a file that contains information about objects and types supported by an ActiveX component. Once you have declared a type library in your Global.asa file, you can use the defined constants in any script in the application.

To declare a type library, use the <METADATA> tag in the Global.asa file for your application. For example, to declare the ADO type library, use the following statements:

<!--METADATA TYPE="typelib"
FILE="c:\program files\common files\system\ado\msado15.dll"
-->

You can then use ADO constants in any script in the same application as the Global.asa file. In the following example, adOpenKeyset and adLockOptimistic are ADO constants:

'Create and Open Recordset Object
Set RsCustomerList = Server.CreateObject("ADODB.Recordset")
RsCustomerList.ActiveConnection = OBJdbConnection
RsCustomerList.CursorType = adOpenKeyset
RsCustomerList.LockType = adLockOptimistic

In previous versions of ASP, some components provided constant definitions in files that had to be included in each ASP file that used those constants. The use of the #include directive to include constant definitions is still supported, but type libraries are generally easier to use and make your scripts more easily upgraded. Components might not provide constant definition files in future releases of ASP.

You can define your own constants. In VBScript, use the Const statement. In JScript, use the var statement. If you want to use your constants on more than one ASP page, put the definitions in a separate file and include them in each ASP page that uses the constants.