ASP Variable - VBScript:-

ASP is not a language in itself. To program ASP you actually need to know the VBScript scripting language. This means that all VBScript variable rules can be applied to your ASP code. This lesson will teach you the basics of Variables in ASP and some good programming conventions.

Declaring Variable in ASP:-

It is a good programming practice to declare all your variables before you use them, even though it is not required. Nearly all programming languages require you to declare variables and doing so also increases your program's readability.

In ASP you declare a variable with the use of the Dim keyword, which is short for Dimension. Dimension in english refers to the amount of space something takes up in the real world, but in computer terms it refers to space in computer memory.

Variables can be declared one at a time or all at once. Below is an example of both methods.

ASP Code:-

'Single Variable Declarations
Dim Var1
Dim Var2
'Multiple Variable Declarations
Dim Var6, Var7, Var8

ASP Variable Naming Conventions:-

Once again, ASP uses VBScript by default and so it also uses VBScripts variable naming conventions. These rules are:

1. Variable name must start with an alphabetic character (A through Z or a through z)
2. Variables cannot contain a period
3. Variables cannot be longer than 255 characters (don't think that'll be a problem!)
4. Variables must be unique in the scope in which it is declared (Basically, don't declare the same variable name in one script and you will be OK).

Assigning Values To ASP Variables:-

Assigning values in ASP is straightforward enough, just use the equals "=" operator. Below we have set a variable equal to a number and a separate variable equal to a string.

ASP Code:-

'Single Variable Declarations
Dim String, Num, Var
Num = 20
String = "Welcome To"
Var = 99
Var = "I changed my variable"
Response.Write("Number = " & Num & "<br />")
Response.Write("String = " & String & "<br />")
Response.Write("Variable = " & Var & "<br />")


Num = 20 myString = Welcome To
myGarbage = I changed my variable

Loading ...

About the Author