Did you find this useful?
Socialize it today.


Declaring Variables in ASP

Saturday, August 11, 2012

tags ASP

A Variable is a very important component for programming. Variables are used to store information in memory so that they can be referenced and changed throughout their life in the program. In most programming languages, variables consist of a name and data type. For example, some variables are stored as Integers or Strings. However, in VBScript, variables are stored as variants. A VBScript variable store almost any kind of data. While this makes development easier, it often leads to challenges in debugging when errors occur.


Declaring a Variable

In most programming languages, it is necessary to declare the variable before using it in the program. VBScript does not require that you declare variables before using them, but it is a good practice to do so. To define a variable, we use the keyword Dim. Dim stands for Dimension and is used to provide information about the size of the variable. Also, when declaring variables, there are certain restrictions on the name of the variable. For example, variables cannot begin with a number, or have spaces within the name. In addition, you cannot name a variable after a keyword. Here is an example on how to declare a variable.


<%
Dim firstName
firstName = "John"
Response.Write (firstName)
%>

You can also declare more than one variable at the same time by separating the names with a comma. For example...

<%
Dim firstName, lastName
firstName = "John"
lastName = "Smith"
Response.Write (firstName & " " & lastName)
%>


Declaring an Array

Arrays are used to store a series of related data items. For example, instead of storing only one name using one variable, we can store multiple names using the same variable name, but as an array. Here is an example.


<%
Dim firstName(2),x
firstName(0) = "John"
firstName(1) = "Sally"
firstName(2) = "Sam"

For x = 0 to 2
  Response.Write(firstName(x) & "<br />")
Next
%>


Lifetime of Variables

A variable's lifetime depends on where the variable is created. Variables declared within a procedure can only be accessed within that procedure. When the procedure is completed, the variable is destroyed. This type of variable is known as a local variable. Since local variables are used within a block of code, it is possible to use the same variable name in different procedures. A good example of a local variable is a variable that is used in a For Loop as shown in the previous example.

If you declare a variable outside a procedure, for example, at the beginning of your ASP block, all of the procedures within your ASP block can have access to the variable. The lifetime of these variables begin when they are declared, and end when the web page is closed.

If you need to have a varible that is accessible from page to page, declare them as session variables or application variables. Session variables are used to store information about a single user, and are available to all pages in one application. It is common to store login information about the user in session variables.


<%
Session("firstName")="John"
%>

When the value is stored in a session variable it can be retreived and modified from any web page in the ASP application. Session variables are stored for 20 minutes by default, but this can be modified. More about session variables in an upcoming article.


<%
Response.Write(Session("firstName"))
%>

Application variables are also available to all of the web pages in an ASP application. Application variables are used to store information about all users in one web application. You can use an application variable to track the number of page visits. More about application variables in an upcoming article.

Please help us spread the word by socializing it today!

email contact us

Did you find something wrong with the information on this page? Please take a moment to report it to us so that we can continue to improve the quality of the information on this site. Click here to report an issue with this page.



Recommended Books & Training Resources

Beginning ASP Databases Sams Teach Yourself Active Server Pages 3.0 in 21 Days ASP In A Nutshell