UFT/QTP – VBScript Tutorial Part -3

Array

A VBScript array is a special type of variable that allows you to store multiple values against a single variable.

Features of Arrays

  • Array in VBScript are Zero-Based.
  • Arrays in VBScript can have a maximum of 60 dimensions.
  • If no value is assigned for an array element it consists of Null value by default.
  • Arrays can not only hold similar kind of data but can have combinations of type of data.

Creating Arrays in VBScript – VBScript arrays are created by

  • First assigning an array object to a variable name
    Syntax: Dim arrayName(No. Of Elements)
    Example: Dim arrayName(3)
  • By Assigning values  to the array
    arrayName(0) = “Array Element 1”
    arrayName(1) = “Array Element 2”
    arrayName(2) = “Array Element 3”

Accessing Array Data

You can access data in an array by referring to the name of the array and the element’s index number.

like – print(arrayName(0))

Example

Dim shoppingList(3)

shoppingList(0) = “Mango”

shoppingList(1) = “Banana”

shoppingList(2) = “Bread”

For each item in  shoppingList

 print(item)

Next

Some In Built Functions Applicable to Arrays –

  • UBound Function – This will return an integer of the highest subscript or item position in the array.
    Example – Print(UBound(shoppingList))  (will return 2 – max index number as per above array)
  • LBound Function – This will return an integer of the lowest subscript or item position in the array.
    Example – Print(LBound(shoppingList))  (will return 0 – lowest index number as per above array)
  • Array() Function – The array function takes comma as delimiter and list as its arguments and returns array to an ordinary variable.
    Example – Dim arryTest
    arryTest = Array(“Element1″,”Element2″,”Element3”)
  • Isarray Function  – This function will return Boolean value True if the variable is an array.
    Example – Dim arryTest
    arryTest = Array(“Element1″,”Element2″,”Element3”)
    if isarray(arryTest) = True Then
    msgbox “This is an array variable”
    else
    msgbox “This is not an array variable”
    End If
  • Erase Function – The Erase function either reinitializes a fixed array or de-allocates the memory used by a dynamic array.
    Example –  Dim fixedArray(3)
    erase  fixedArray

Types of Arrays – There are two types of array in VBScript.

Static or Fixed Size ArraysAn array that has fixed number of elements within it is called as fixed or static array. The size of a static array does not change.

Example : Dim myarray(10) (myarray variable is created as fixed elements)

Dynamic Arrays – You can also declare an array whose size changes during the time your script is running. This is called a dynamic array. Dynamic arrays are created in the same way as static arrays but the only difference is that no upper bound will be define in the declaration.

The array is initially declared using either Dim Statement or using ReDim statement.

ReDim: – Redim statements is used to “re-dimension” the array to as many elements you epecify.

Example – Dim MyArray()

Redim  MyArray(25)

……..

…….

Redim  MyArray(50)

Preserve Statement – The Preserve keyword is used to preserve the contents of the array as the resizing takes place. If the preserve keyword is not used then all the previous array elements will be lost.

Example –  Dim MyArray()

ReDim MyArray(30)

……..

ReDim preserve MyArray(50)

Multiple Dimensional Arrays – You can have as many 60 dimensions, although most people can’t comprehend more than three or four dimensions.

Example –  Dim MyTable (5,10) (define two dimension array)

VBScript – Strings

Strings are a sequence of characters, which can consist of alphabets or numbers or special characters or all of them. A variable is said to be a string if it is enclosed within double quotes ” “.

Syntax
variablename = “string”
Examples
str1 = “string”   ‘ Only Alphabets
str2 = “132.45”   ‘ Only Numbers
Str4 = “Asc23@#”  ‘ Has all the above
String Functions

Function Name Description
InStr Returns the first occurrence of the specified substring. Search happens from left to right
InstrRev Returns the first occurrence of the specified substring. Search happens from Right to Left.
Lcase Returns the lower case of the specified string.
Ucase Returns the Upper case of the specified string.
Left Returns a specific number of characters from the left side of the string.
Right Returns a specific number of characters from the Right side of the string.
Mid Returns a specific number of characters from a string based on the specified parameters.
Ltrim Returns a string after removing the spaces on the left side of the specified string.
Rtrim Returns a string after removing the spaces on the right side of the specified string.
Trim Returns a string value after removing both leading and trailing blank spaces.
Len Returns the length of the given string.
Replace Returns a string after replacing a string with another string.
Space Fills a string with the specified number of spaces.
StrComp Returns an integer value after comparing the two specified strings.
String Returns a String with a specified character the specified number of times.
StrReverse Returns a String after reversing the sequece of the characters of the given string.

VBScript – Procedures

What is a Function?
A function is a group of reusable code which can be called anywhere in your program. This eliminates the need of writing same code over and over again. This will enable programmers to divide a big program into a number of small and manageable functions.

Apart from inbuilt Functions, VBScript allows us to write user-defined functions as well.
Function Definition
The most common way to define a function in VBScript is by using the Function keyword, followed by a unique function name and it may or may not carry a list of parameters and a statement with a End Function keyword, which indicates the end of the function.

The basic syntax is shown below:
Function Functionname(parameter-list)
statement
End Function
Example – 
Function sayHello()
msgbox(“Hello there”)
End Function
Calling a Function
To invoke a function somewhere later in the script, you would simple need to write the name of that function with the Call keyword.
Example – Call sayHello()
Function Parameters
Till now, we have seen function without a parameter, but there is a facility to pass different parameters while calling a function. These passed parameters can be captured inside the function and any manipulation can be done over those parameters. The Functions are called using the Call Keyword.

Function sayHello(name, age)
msgbox( name & ” is ” & age & ” years old.”)
End Function
Call sayHello(“Tutorials point”, 7)
Returning a Value from a Function
A VBScript function can have an optional return statement. This is required if you want to return a value from a function.
Example –
Function concatenate(first, last)
Dim full
full = first & last
concatenate = full  ‘Returning the result to the function name itself
End Function
‘ Here is the usage of returning value from function.
dim result
result = concatenate(“Zara”, “Ali”)
msgbox(result)
Sub Procedures
Sub Procedures are similar to functions but there are few differences.
• Sub procedures DO NOT Return a value while functions may or may not return a value.
• Sub procedures can be called without call keyword.
• Sub procedures are always enclosed within Sub and End Sub statements.
Example –
Sub sayHello()
msgbox(“Hello there”)
End Sub

Creating Library Files

Libarary is nothing but a file which contains function procedures and sub procedures. These library files can be associated to any script QTP, so that the script in QTP can call or reuse the procedures present in the library file.

Library files can have 3 extensions as below

  • .txt
  • .vbs
  • .qfl

Leave a Reply

Your email address will not be published. Required fields are marked *