VBScript stands for Visual Basic Script and is developed by Microsoft.
Quick Test Professional (QTP) uses VBScript as its scripting language. This article talks about:
- How to Run VBScript code - Variables & Variable Declaration - Types of Variable - Comments in VBScript
How to Run VBScript code
VBScript doesn’t require any special editor to write and execute script. VBScript code can be written and executed with Notepad. Simply write your code in notepad and save your script with .vbs extension. Make sure that while saving you choose value All Files in the drop down Save as type.
Once your script is saved, just double click on the script file to run it.
Variable & Variable Declaration
A variable is a place holder for some value. VBScript is not strict about declaring a variable before using it. A variable is automatically declared when it is used anywhere in the script. This is called implicit declaration. But it is not considered a good practice and may introduce hard to debug issues in the code. E.g.:
a=5 b=7 msgbox a+b
In the above code snippet, we didn’t declare variables a and b before using them.
To make declaration of variables mandatory, we use command Option Explicit. This is called explicit declaration.
If you are using Option Explicit, it must be the first statement in your VBScript program.
Try using Option Explicit command with the above code snippet.
Option Explicit a=5 b=7 msgbox a+b
Now run this script. You would get variable is undefined error message and your script won’t run because Option Explicit command expects that you would declare the variables before using them.
Variables in VBScript are declared using command Dim. E.g.:
Dim Dim <varname1>, ….so on
Now let’s fix the above code snippet by using variable declaration.
Option Explicit Dim a, b a=5 b=7 msgbox a+b
Run the script now and you script would run successfully.
Variable Naming Restrictions-
– Variable name must begin with and alphabet.
– Variable name cannot contain an embedded period symbol.
– Variable name must not exceed 255 characters.
Types of Variables
VBScript has two types of variables:
1. Scalar Variables – A variable that can hold only one value at a time is called scalar variable. All the variables that we used in above sample scripts were scalar variables. They are declared as:
Dim varname1, varname2,….
2. Arrays: A variable that can hold multiple values at a time is called an array. To declare array variable, variable name is followed by parenthesis & parenthesis contains dimensions of the array and size for each dimension. E.g.:
Here firstArray is an array variable of size 6 that it can hold six values at a time.
You might be wondering that we gave the value 5 in parenthesis then why is it an array of size 6. It’s because arrays in VBScript are zero index arrays that means array index starts at 0. So the size of the arrays would be value mentioned in the parenthesis + 1 hence it can contain 6 elements.
Here is how you assign value to each element in the array:
firstArray(0) = 25 firstArray(1) = 23 firstArray(2) = 45 firstArray(3) = 782 firstArray(4) = 226 firstArray(5) = 96
The array we declared above is a single dimension array. VBScript allows arrays with up to 60 dimensions.
Here is how you declare a multi dimension array:
Dim multiDimenArray(5, 9)
Here the first value in the parenthesis is first dimension of the array and represent number of rows. In this case, number of rows are 6.
The second value in the parenthesis is the second dimension of the array and represents no of columns. In this case, no of columns are 10.
Few more examples are:
Dim array1(2, 4, 5) – Three dimension array Dim array2(12, 4, 6, 8) – Four dimension array Dim array3(2, 14, 5, 8, 45) – Five dimension array
Types of array: VBScript has two types of array:
1. Static array: An array whose size can’t be changed at run time.
2. Dynamic array: An array whose size can be changed at run time.
Static arrays are declared with Dim followed by variable name with parenthesis having a value in the parenthesis. The array firstArray we declared above was a static array.
Dynamic arrays are also declared in the same way except that parenthesis doesn’t contain any value. Parenthesis must be blank. To give a size to the dynamic array and make it a usable, Dim statement must be followed by a ReDim statement. Lets see an example:
Dim dynArray() Redim dynArray(5)
So now dynArray is a dynamic array of size 6. You can use Redim statement any number of times after this to resize this array.
Redim dynArray(1) Redim dynArray(4)
What happens to the existing data when you resize a dynamic array: If you resize the array using Redim, the data in the array is not retained. If you wish you to retain the existing data in the array, using keyword Preserve while resizing the array:
Our last statement resized the array to size 4. Let’s resize it to size 10 using Preserve along with Redim.
Redim Preserve dynArray(9)
But if you resize an array to a smaller size than it was initially, you are bound to lose the data.
NOTE: If you are using Preserve keyword:
1. You can only resize last dimension of the of a multi dimension array. In the above example, dynArray had only one dimension so you could resize it. Lets take an example of multi dimension array:
Dim multiDimArray() ReDim multiDimArray(2, 4, 5) Redim Preserve multiDimArray (2, 4, 6)
You can only resize the last dimension here, if you try to resize any other dimension you will get a run time error.
2. You cannot increase or decrease the number of dimension. So this would not work:
Dim multiDimArray() ReDim multiDimArray(2, 4, 5) Redim Preserve multiDimArray (2, 4)
How to place comments in the VBScript:
Comments in VBScript
Comments are non executable statements in the code that provide info about the code so that it is easy to maintain
1. Using apostrophe symbol: Any text on the same line following apostrophe symbol is treated as a comment. E.g.:
‘This is a comment.
2. Using Rem keyword: Any text on the same line following keyword Rem is treated as a comment. E.g.:
Rem This is a comment too.
That’s all for this article. Next article will talk about data types & operators in VBScript.