VBScript
has only one data type Variant, which can store different types
of data. So, a variable declared in VBScript can store any type of data.
Note: There is a way to assign a value to the variable in declaration statement as follows -
While scripting in QTP, it is better to maintain some sort of coding standards for all variables we declare in order to easily figure out what kind of data each variable stores.
Ex:
Declaring Variables:
In
VBScript, we have only one statement 'Dim'
for declaring all types of variables.
Ex: Dim
a
Remember,
we cannot directly assign a value in the variable declaration statement itself.
We can assign values to variables in the next statements after declaring
variables.
1. Dim a=1 (Wrong)
2.
Dim a
a=1 (Correct)
Note: There is a way to assign a value to the variable in declaration statement as follows -
Dim a
: a=1
We
can also declare variables directly in the script without using 'Dim' statement
as –
Name = "Sample Name"
But,
it is not a good practice to follow this way.
Let
us assume that we have created a variable 'temp' without using 'Dim' statement
in our script –
temp = 1
If
we want to use the same variable in other statements and misspell like 'twmp' –
twmp = temp+1
then
QTP considers 'twmp' as a new variable declaration.
So,
always have a practice to declare variables using 'Dim' statement.
Here,
you should have definitely got a doubt like, what if we misspell even after
declaring variables using ‘Dim’ statement -
Dim
temp
temp = 1
twmp = temp+1
In
this case also, QTP considers 'twmp' as a new variable declaration.
So,
here comes a new statement 'Option Explicit' into
the picture in VBScript.
Option Explicit:
This
statement throws an error if we declare variables without using 'Dim' statement
In
the above example, if we put 'Option Explicit' statement at the top then the
script does not execute and throws an error 'Variable is undefined: twmp'
Option Explicit
Dim temp
temp = 1
twmp = temp+1
temp = 1
twmp = temp+1
Output: Variable
is undefined: twmp
Array Variables:
Array
variable declaration in VBScript is same as in other programming languages. But
the size of the array differs in VBScript from other programming languages.
Ex:
Dim array(5)
Generally,
the size of the above array would be 5. But in VBScript, it is 6.
So,
in VBScript arrays declaration, the number we enter inside parenthesis is the
last index of that array, but not the size of that array.
Starting
from index ‘0’, the above array would have a total of 6 elements in it - array(0), array(1), array(2), array(3), array(4),
array(5).
Similarly,
two dimensional arrays can be declared as – Dim array(3,5)
So,
the above two dimensional array would have 4 rows and 6 columns in it (4x6
array).
Redim:
Redim statement is
used to resize the array variables that were already created using Dim
statement.
We
use this statement when we require dynamic arrays in our scripts.
Ex:
Dim array(2)
array(0) = "First Index"
array(1) = "Second Index"
array(2) = "Third Index"
Redim array(3)
array(3) = "Fourth Index"
In
this case, the data of the array before 'Redim' statement would be erased. So,
now the array has only one element which is in the last index i.e.,
"Fourth Index"
If
we want to resize the array without losing its data, we should use 'Preserve' keyword.
Ex:
Redim Preserve array(3)
Note:
In case of two-dimensional arrays, we can resize only the second dimension of
the array using Redim, but not the first dimension.
For
example, array(10,10) can be resized as array(10, New_Size) but not as
array(New_Size, 10).
Coding Standards:
While scripting in QTP, it is better to maintain some sort of coding standards for all variables we declare in order to easily figure out what kind of data each variable stores.
Start
the variable name with the data type followed by variable name with the
starting letter of the variable name in Capitals.
Ex:
Integer
Variables - intVar / iVar (int - data type,
Var - Variable name with starting letter in capitals)
String
Variables - strVar / sVar
Array
Variables - arrVar
Boolean
Variables - blnVar
No comments:
Post a Comment