In the previous chapter we have started the use of the VBScript in HTML documents. In this chapter we’ll discuss the datatypes, variables and Arrays in the VBScript.
Data types
Basically in VBScript there is only on data type called as ‘variant’ that can hold either the numeric or string value. But you can categorized it according to the information that it contains in the following subtypes:
Boolean: value will be true or false
Byte: Integer value in the range of 0 to 255.
Integer: Integer value in the range of -32,768 to 32,767.
Long: Integer value in the range: -2,147,483,648 to 2,147,483,647.
Single: a single-precision, floating-point number in the range -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values.
Double: a double-precision, floating-point number in the range -1.79769313486232E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values.
Date: a number that represents a date.
String: a variable-length string (up to approximately 2 billion characters in length).
Object: an object.
Error: an error number.
Null: no valid data.
Declaring variables
In VBScript we declare a variable using the ‘Dim’ keyword as:
Dim myname
You can declare multiple variable with single Dim statement as :
Dim myname, firstname, lastname
Variable should have a unique name and can be assign with the value:
Dim myname
myname = "Rakesh"
A variable’s scope(private or public) is determined by where you declare it. if you declare the variable within the method than it will accessible only with in the method.
Arrays
Array is the collection of items just as in other programming or scripting languages and can be declared with the variable name along with the parentheses ( ).
Dim cities(5)
It means that the variable ‘cities’ contains the six value. The number within the parentheses ( ) refers the length of the items because it is zero based so cities will contain the 6 item.
To assign the data to each of the elements of the array using an index into the array like as follows:
cities(0) = "New Delhi"
cities(1) = "Landon"
cities(2) = "New York"
cities(3) = "Paris"
cities(4) = "Kolkata"
cities(5) = "Tokyo"
Here one question raises that can be change the length of the array after declaring it?. Yes we can do that by using the Redim statement. ReDim statement resizes the array to new length and with the help of ‘Preserve ‘ keyword you can keep the earlier values.
Redim Preserve cities(6)
cities(5) = "Mumbai"
You can also declare array with multiple dimensions by separating an array’s size numbers in the parentheses with commas. for the example:
Dim myNumbers(1,2)
Than myNumber will contain the values in the 2 rows and 3 columns like as:
myNumber(0,0) = 45
myNumber(0,1) = 67
myNumber(0,2) = 48
myNumber(1,0) = 56
myNumber(1,1) = 48
myNumber(1,2) = 34
Next chapter:Using VBScript in HTML document
Previous chapter:Learn basics of the VBScript – The VbScript Tutorial