VBSCRIPT VARIABLES

  • Variables are "containers" for storing information.
  • VBScript variables are used to hold values or expressions.
A variable can have a short name, like x, or a more descriptive name, like carname.

Rules for VBScript variable names:

  • Must begin with a letter 
  • Cannot contain a period (.)
  • Cannot exceed 255 characters
In VBScript, all variables are of type variant, that can store different types of data.

Declaring (Creating) VBScript Variables:

Creating variables in VBScript is most often referred to as "declaring" variables.
You can declare VBScript variables with the Dim, Public or the Private statement.
Like this:
Dim x
Dim carname

Option Explicit statement:

If you misspell for example the "carname" variable to "carnime", the script will automatically create a new variable called "carnime".  To prevent your script from doing this, you can use the Option Explicit statement. This statement forces you to declare all your variables with the dim, public or private statement.
Put the Option Explicit statement on the top of your script. Like this:
Option Explicit
Dim carname

Assigning Values to Variables:

assign a value to a variable like this:
carname="Volvo"
x=10
The variable name is on the left side of the expression and the value you want to assign to the variable is on the right. Now the variable "carname" has the value of "Volvo", and the variable "x" has the value of "10".

Lifetime of Variables:

How long a variable exists is its lifetime.
When we declare a variable within a procedure, the variable can only be accessed within that procedure. When the procedure exits, the variable is destroyed. These variables are called local variables. You can have local variables with the same name in different procedures, because each is recognized only by the procedure in which it is declared.
If we declare a variable outside a procedure, all the procedures on your page can access it. The lifetime of these variables starts when they are declared, and ends when the page is closed. These variables are called global variables.

VBScript Array Variables:

An array variable is used to store multiple values in a single variable.

Example:

An array containing 3 elements is declared as:
Dim names(2)
The number shown in the parentheses is 2. We start at zero so this array contains 3 elements. This is a fixed-size array.
We can assign data to each of the elements of the array like this:
names(0)="aaa"
names(1)="bbb"
names(2)="ccc"
the data can be retrieved from any element using the index of the particular array element we want. Like this:
mother=names(0)
we can have up to 60 dimensions in an array. Multiple dimensions are declared by separating the numbers in the parentheses with commas.

Example:

A two-dimensional array consisting of 5 rows and 7 columns is declared as:
Dim table(4,6)
Assign data to a two-dimensional array:

Example

<html>
<body>

<%
Dim x(2,2)
x(0,0)="Volvo"
x(0,1)="BMW"
x(0,2)="Ford"
x(1,0)="Apple"
x(1,1)="Orange"
x(1,2)="Banana"
x(2,0)="Coke"
x(2,1)="Pepsi"
x(2,2)="Sprite"
for i=0 to 2
    response.write("<p>")
    for j=0 to 2
        response.write(x(i,j) & "<br />")
    next
    response.write("</p>")
next
%>

</body>
</html>






No comments:

Post a Comment