About Me

Friday 20 April 2012

ASP Variables - VBScript


ASP Variables - 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 a 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 myVar1
Dim myVar2
'Multiple Variable Declarations
Dim myVar6, myVar7, myVar8
%>

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).

ASP - 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 myString, myNum, myGarbage
myNum = 25
myString = "Hello"
myGarbage = 99
myGarbage = "I changed my variable"
Response.Write("myNum = " & myNum & "<br />")
Response.Write("myString = " & myString & "<br />")
Response.Write("myGarbage = " & myGarbage & "<br />")
%>

Display:

myNum = 25 myString = Hello
myGarbage = I changed my variable

0 comments:

Post a Comment