UFT Tutorial 23

UFT Class 23 (VBScript variables, Operators-1)

III) VBScript Variables

-----------------
1) What is Variable?


A named memory location to store the data.

Two types of Memory in Computer Environment:

i) Primary Memory - RAM

ii) Secondary Memory - HDD, DVD, USB drives etc...
---------------------
2) Declaration of Variables


Variables can be declared using either Public or Private or Dim statements.

How to declare:

Syntax:

Dim VariableName

Or

Dim Variable1Name, Variable2Name, Variable3

Ex:

Dim a

Or

Dim a, b, c
------------------------------
3) Implicit and Explicit declaration of Variables

Ex:

Dim a
a = 100 'Explicit Variable
b = 200 'Implicit Variable
Msgbox a + b
--------------------
Dim Tickets, Price, Total
Tickets = 7
Price = 100
Total = Tickets * Priee 'Problem with Implicit variables
Msgbox Total
-----------------------------
Option Explicit 'It forces declaration of all variables
Dim Tickets, Price, Total
Tickets = 7
Price = 100
Total = Tickets * Priee 'Problem with Implicit variables
Msgbox Total
----------------------------------------------
4) Assigning values to Variables

Two types

i) Initialization

Ex:

a = 100

ii) Reading
                from Input devices
                from files (Text, Excel)
                from databases
                from Application Objects

Ex:
Dim num1, num2
num1 = 100 'Initialization
num2 = InputBox("Enter Num2 Value") 'Reading
Msgbox "Addition of num1, num2 is: " & num1 + num2
---------------------------------
5) Usage of variables

Dim a, b
a = 100 'Holding the Data

a = 10 ^ 3 'Storing the data that return by a program
Msgbox a

a = Date 'Storing the data that return by a Function
Msgbox a
'Storing Object reference
Set a = CreateObject("Scripting.FileSystemObject")
Msgbox VarType(a) '9 for Automation Object

'As Parameter
For b = 1 To 5 Step 1
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON" Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set b 'b is parameter
Wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
Next     
--------------------------------------------------
6) Variable Naming Restrictions

i) Variable names should start with Alfa bytes

Ex:
Dim abc 'Correct
Dim ab7 'Correct
Dim 7bc 'Incorrect

ii) Variable cannot contain embedded periods

Ex:
Dim abc 'Correct
Dim ab c 'Incorrect
Dim ab-c 'Incorrect
Dim ab*c 'Incorrect
Dim ab.c 'Incorrect
Dim ab_c 'Correct

iii) Variable names must not exceed 255 characters

iv) Must be unique in the scope of declaration

Dim a, b, c 'Correct
Dim d, e, f 'Correct
Dim g, h, A 'Incorrect

v) Should not use reserved words as variable names

Ex:
Dim For, While
-------------------------------------
7) Scope of Variables

i) Script Level Variables (It can be used for entire script)

ii) Procedure / Function Level Variables (It can be used within the Function only)

Ex:

Dim a, b, c 'Script Level Variables
a = 10
b = 20
c = a + b
Msgbox c '30 (10 + 20)

Function xyz()
                Dim d, e 'Function Level variables
                d = 50
                e = a + d
                Msgbox e
End Function
Call xyz '60 (10 + 50)
Dim f, g 'Script Level Variables
f = 70
g = b + d + f
Msgbox g '90 (20 + 0 + 70)
----------------------------
Option Explicit
Dim a, b, c 'Script Level Variables
a = 10
b = 20
c = a + b
Msgbox c '30 (10 + 20)

Function xyz()
                Dim d, e 'Function Level variables
                d = 50
                e = a + d
                Msgbox e
End Function
Call xyz '60 (10 + 50)
Dim f, g 'Script Level Variables
f = 70
g = b + d + f
Msgbox g 'Error
-------------------------------
8) Types of Variables

i) Scalar Variables (To store single value)

ii) Array variables (To store series of values at a time based on size of the Array)

Note: VBScript Arrays having Zero based index.
-------------------------
9) Declaration of Arrays

Syntax:

Dim ArrayVariblename (size in number)

Ex:

Dim a (3) ' we can store four values
-----------
Ex 2:
Dim a(3), b
a(0) = 10
a(1) = 20
a(2) = 30
a(3) = 40

b = a(1) + a(2)
Msgbox b '50
---------------------
Dim a(3), b
a(0) = 10
a(1) = 20
a(2) = 30
a(3) = "abc"

b = a(1) + a(2)
Msgbox b '50

Note: we can assign different types of data also.
----------------------------------
Dim a(3), b
a(0) = 10
a(1) = 20
a(2) = 30
a(3) = 40
a(4) = 50 'Error (Subscript Out of range)
b = a(1) + a(2)
Msgbox b '50
------------------------------------
10) Dynamic and Dimensional Arrays

Dynamic Array:
Ex:
Dim a, b(3), c()
ReDim c(2)
c(0) = 10
c(1) = 20
c(2) = 30
a = c(0) + c(2)
Msgbox a '40
ReDim c(4)
c(3) = 40
c(4) = 50
a = c(2) + c(4)
Msgbox a '50
------------------------
Preserve Keyword:
Dim a, b(3), c()
ReDim c(2)
c(0) = 10
c(1) = 20
c(2) = 30
a = c(0) + c(2)
Msgbox a '40
ReDim Preserve c(4)
c(3) = 40
c(4) = 50
a = c(2) + c(4)
Msgbox a '80
------------------------
Dimensional Arrays
Ex:
Dim a, b(4), c(), d(4, 5)
d(0, 0) ="UFT"
--------------------------
11) Assigning Series of values to Array variables


i) Using Array Function

Ex:
Dim a
Msgbox IsArray(a)'False
a = Array(100, 200, 300, "India", 1.22, #10/10/2010#)
Msgbox IsArray(a) 'True

Msgbox a(1) '200
Msgbox a(3) 'India

Msgbox UBound(a) '5

ii) using Split Function
Ex:
Dim a, b
a = "VB Script Language"
Msgbox IsArray(b) 'False
b = Split(a)
Msgbox IsArray(b) 'True

Msgbox b(1) 'Script

Msgbox UBound(b) '2
-------------------------
Dim a, b
a = "VB@Script@Language"
Msgbox IsArray(b) 'False
b = Split(a, "@")
Msgbox IsArray(b) 'True

Msgbox b(1) 'Script

Msgbox UBound(b) '2
--------------------------
Dim a, b
a = "VB@$%Script@$%Language"
Msgbox IsArray(b) 'False
b = Split(a, "@$%")
Msgbox IsArray(b) 'True

Msgbox b(1) 'Script

Msgbox UBound(b) '2
-------------------------------------
IV) VBScript Operators


Operators are used to perform mathematical, comparison and logical operations.

Operator precedence:


It is Operator evolution process, generally VBScript evaluates operators from left to right.
But any high priority operator is there in right side, VBScript first evaluate high priority operator then general rule.

Ex:
Msgbox 10 + 4 * 2 '18
Msgbox (10 + 4) * 2 '28
----------------------------
Categories of Operators:

-----------------------
i) Arithmetic Operators

ii) Comparison Operators

iii) Logical Operators

---------------------
Concatenation operators (* Part of Arithmetic Operators)

i) Arithmetic Operators

----------------------------
a) Exponentiation ^

b) Multiplication *

c) Division /

d) Integer division \

e) Mod Operator

f) Addition +

g) Subtraction -

h) Concatenation &
------------------------------

0 comments:

Post a Comment