UFT Class 23 (VBScript variables, Operators-1)
III) VBScript Variables
-----------------
1) What is Variable?
A named retention place to shop the data.
Two types of Memory inward Computer Environment:
i) Primary Memory - RAM
ii) Secondary Memory - HDD, DVD, USB drives etc...
---------------------
2) Declaration of Variables
Variables tin halt hold upwards 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 in addition to Explicit annunciation 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 amongst Implicit variables
Msgbox Total
-----------------------------
Option Explicit 'It forces annunciation of all variables
Dim Tickets, Price, Total
Tickets = 7
Price = 100
Total = Tickets * Priee 'Problem amongst 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 ^ iii 'Storing the information that provide yesteryear a program
Msgbox a
a = Date 'Storing the information that provide yesteryear 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 v 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 cause amongst Alfa bytes
Ex:
Dim abc 'Correct
Dim ab7 'Correct
Dim 7bc 'Incorrect
ii) Variable cannot incorporate 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 non transcend 255 characters
iv) Must hold upwards unique inward the compass of declaration
Dim a, b, c 'Correct
Dim d, e, f 'Correct
Dim g, h, Influenza A virus subtype H5N1 'Incorrect
v) Should non piece of job reserved words every bit variable names
Ex:
Dim For, While
-------------------------------------
7) Scope of Variables
i) Script Level Variables (It tin halt hold upwards used for entire script)
ii) Procedure / Function Level Variables (It tin halt hold upwards used inside 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 shop unmarried value)
ii) Array variables (To shop serial of values at a fourth dimension based on size of the Array)
Note: VBScript Arrays having Zero based index.
-------------------------
9) Declaration of Arrays
Syntax:
Dim ArrayVariblename (size inward number)
Ex:
Dim a (3) ' nosotros tin halt shop 4 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: nosotros tin halt assign dissimilar types of information also.
----------------------------------
Dim a(3), b
a(0) = 10
a(1) = 20
a(2) = 30
a(3) = 40
a(4) = l 'Error (Subscript Out of range)
b = a(1) + a(2)
Msgbox b '50
------------------------------------
10) Dynamic in addition to 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, comparing in addition to logical operations.
Operator precedence:
It is Operator development process, to a greater extent than oft than non VBScript evaluates operators from left to right.
But whatsoever high priority operator is at that spot inward right side, VBScript offset evaluate high priority operator in addition to thence full general rule.
Ex:
Msgbox 10 + 4 * two '18
Msgbox (10 + 4) * two '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 partition \
e) Mod Operator
f) Addition +
g) Subtraction -
h) Concatenation &
------------------------------