Basic of VB Scripting Concepts to Learn QTP
It should be noted that this article is not an exhaustive list of all the VB Script features. Consider this as a place that will introduce you to certain key concepts to guide you while you attempt to self-learn the language. Since the expert view of QTP is a VB Script editor, it is a must that we try to understand VBscript for QTP before we move on to our articles on QTP Expert View.VBScript data types and subtypes:
Data types:
1) There is only one data type: Variant. It can store different kinds of information based on the context in which it is used.2) If used in a numeric context it is a number or a string if used in a string expression.
3) If a number has to behave as a string we could enclose it within “ “.
4) There are various subtypes to a variant. You can explicitly specify these subtypes to achieve a clear definition for your data. The below is a screen shot from the VB User guide that shows all the subtypes of data that can be used:
(click on image to enlarge)
5) Conversion functions can be used to convert one subtype of data into another.
6) Since it is the only data type available, all the return values from a function are variants.
Here are different VBScripting examples you can try on your own.
Variables:
1) A variable is nothing but a space in the computer’s memory that can store certain information. This information is bound to change from time to time. Where the information goes physically is immaterial but when needed, it can be accessed or changed by addressing the name of the variable.E.g: If there is statement that you want to run a number of times, you could use a variable to contain that count. Say X. X is a variable that can be used to stored, change and use the space in the memory where we want to keep the count.
2) All variables are of the datatype Variant.
3) Declaring a variable prior to its use is optional, although it’s a good practice to do so.
4) To make the declaration mandatory there is a “Option Explicit” Statement available.To declare variables:
Dim x – This declares x
Dim x, y, z – This declares multiple variables
X=10 – This is how a value is assigned. As a general rule, the variable is the left hand side component and the right is its value.
X=”Swati” – this is the way a string value is assigned.
To make declarations mandatory this is how the code has to be written:
Option Explicit
Dim x, stri
If Option explicit statement was not used, we could have directly written:
x=100
stri=”Swati”
and it would not have thrown an error.
5) Naming convention: Names must start with an alphabetic character, must be unique, cannot contain and embedded period and cannot exceed 255 chars.
6) A variable containing a single value is a scalar variable and the one that has more than one is an array.
7) A one dimensional Array can be declared as: Dim A(10). All the arrays in VB Script are zero based that means the array index starts from 0 through the number declared. That means, our array A has 11 elements. Starting from 0 to 10.
9) There is also a dynamic array whose size can change during run time. These arrays can be declared using dim or redim statements.
If an array is declared as Dim A(10) and during run time if we need more space we can so the same by using the statement: redim A(10). There is a “Preserve” statement that can be used in conjunction with the redim statement.
Dim A(10,10)
……
….
Redim preserve A(10,20)
This piece of code shows how we do it. Initially A is a 11 by 11 array. Then we are resizing it to be a 11 by 21 array and the preserve statement will make sure that the data that is previously contained in the array is not lost.
Constants:
- As the name implies a constant is nothing but an unchanging value in a program that is assigned a name.
- They can be declared by prefixing “Const” to a name.
- Eg: Const a=”10” or Const Astr=”Swati”.
- This value cannot be changed accidentally while the script is running.
Operators:
Some of the important operators that are most commonly used are:- String concatenation: & (Eg: Dim x=”good”&”day”, so x contains “goodday”
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division(/)
- Logical negation (Not)
- Logical conjunction (And)
- Logical disjunction ( Or)
- Equality(=)
- Inequality (<>)
- Less than (<)
- Greater than(>)
- Less than or equal to(<=)
- Greater than or equal to (>=)
- Object equivalence(Is)
The operator precedence rules are:
- Multiplication or Division take precedence over addition or subtraction
- If multiplication and division exist in the same expression, then left to right order is considered
- If Addition and subtraction occur in the same expression,then too, left and right order is taken into consideration.
- The order can be over ridden by using parenthesis. In this case, the expression within the parenthesis is executed first.
- & operator takes precedence after all arithmetic operators and before all logical operators.
No comments:
Post a Comment