VBScript Data Types and Variables


VBScript Data Types and Variables

1) VBScript Data Types 

a) VBScript supports implicit declaration of Data types
 

> VBScript only Data type is Variant, VBScript variables can hold any type of data, based on usage of the data VBScript considers data sub-types.

Dim a
a ="London" 'String type
------
-----
----
a = 100 'Integer type
------
-----
----
a = 1.23 'Double type
------
-----
----
a = #10/10/2010# 'Date type data
-----------------------
b) Check Data sub types using VarType Function
 

VarType Function returns Constant based Result

Ex:

8 for String type

2 for Integer

5 for Double

7 for Date
etc...
------------------
Example:

Dim a
Msgbox VarType(a) '0 for Empty or Uninitialized

a = 100
Msgbox VarType(a) '2 for Integer

a = "London"
Msgbox VarType(a) '8 for String

a = "100"
Msgbox VarType(a) '8 for String

a = 1.45
Msgbox VarType(a) '5 for Double

a = #10/10/2010#
Msgbox VarType(a) '7 for Date

Set a = CreateObject("Scripting.FileSystemObject")
Msgbox VarType(a) '9 for Object
-------------------------------------
Types of Result in Computer Programming
 

3 Types of Results

i) Value based Result

Ex:

5 + 3 = 8

10 * 3 = 30

ii) Boolean Result

2 way comparison (True /False)

Msgbox IsNumeric(100) 'True
Msgbox IsNumeric("Abcd") 'False

3 way comparison (-1/1/0)

Str1 > Str2 then 1
Str1 < Str2 then -1
Str1 = str2 then 0

Msgbox StrComp("UFT", "uft") '-1
Msgbox StrComp("UFT", "UFT") '0
Msgbox StrComp("uft", "UFT") '1

'A-Z (65 to 90)
'a -z (97 to 122)
--------------------------
iii) Constant based result

Msgbox VarType("xyz") '8
-----------------------------
c) Convert the Data from one sub type to another using Conversion Functions.
 

Assign values to Variables 2 types

i) Initialization

Ex:

x = 100

ii) Reading
    Read using Input devices
    Read from files/databases
    Read from Application objects

Note: Whenever we read data then VBScript considers the data as String type data
in order to perform Mathematical operations then we need to convert the data.

Example:

Initialization Vs. Reading

Dim num1, num2
num1 = 100
num2 = 200
Msgbox num1 + num2

Dim num1, num2
num1 = InputBox("Enter num1 value")
num2 = InputBox("Enter num2 value")
Msgbox num1 + num2
------------------------
'Convert String type data to Integer sub type
Dim num1, num2
num1 = InputBox("Enter num1 value")
num2 = InputBox("Enter num2 value")
Msgbox Cint (num1)  + Cint (num2)

Note: We cannot convert Alfa bytes to Integer.

'Convert String type data to Double sub type
Dim num1, num2
num1 = InputBox("Enter num1 value")
num2 = InputBox("Enter num2 value")
Msgbox Cdbl (num1)  + Cdbl (num2)
-------------------------------------------
'Read data from Application Objects

Dim Tickets, Price

Tickets = Window("Flight Reservation").WinEdit("Tickets:").GetROProperty("text")
Msgbox Tickets '6
Msgbox VarType(Tickets) '8
Msgbox VarType(Cint(Tickets))'2

Price = Window("Flight Reservation").WinEdit("Price:").GetROProperty("text")
Msgbox VarType(Price) '8
Msgbox VarType(Cdbl(Price)) '5
--------------------------------------
2) VBScript Variables


a) What is variable?
 

A named memory location to store the Data.

Two types of memories in Computer Environment

i) Primary memory (RAM)

ii) Secondary memory (ROM -HDD, DVD, USB Drive etc...)

Note: Variables store in Primary memory
---------------------------------
b) Implicit and explicit Declaration of Variables
 

Using either Public or Private or Dim keywords we can declare variables

Syntax:

Dim varaibleName

Dim varaible1Name, varaible2Name, varaible3Name

Or

Dim varaible1Name
Dim varaible2Name
Dim varaible3Name

Example:

Dim a
a=100
Msgbox a

Dim a, b, c
a=100
b=200
c=300
Msgbox a+b+c

Dim a
Dim b
Dim c
a=100
b=200
c=300
Msgbox a+b+c
--------------------
Implicit and Explicit declaration of Variables
 

Dim a
a=100 'Explicit Variable
b=200 'Implicit Variable
Msgbox a + b

Note: Explicit declaration of Variables is Best practice.

If we use implicit variables, user may misspell variables.

Dim Tickets, Price, Total
Tickets = 7
Price = 100
Total = Tickets * Priee
Msgbox Total
-------------------------------
Option Explicit
It forces declaration of all variables in the script.

Option Explicit
Dim Tickets, Price, Total
Tickets = 7
Price = 100
Total = Tickets * Price
Msgbox Total
------------------------------
c) Assign values to Variables
 

Assign values to variables is 2 types

i) Initialization

ii) Reading

Example:
Dim num1, num2
num1 = 100 'Intialization
num2 = Inputbox("Enter num2 Value") 'Reading
Msgbox num1 + num2
--------------------------------------
d) Usage of Variables
 

Dim a, OrderNumber

a = 100 'To hold the Data

a = 10 ^ 3 * 7 'Storing the value that returned by a program
Msgbox a '7000

a = Now ''Storing the value that returned by a Function
Msgbox a

'Storing Object Reference
Set a = CreateObject("Scripting.FileSystemObject")

'As parameter
For OrderNumber = 1 To 10 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 OrderNumber
wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
Next
--------------------------------------
e) Variable naming restrictions
 

i) Variable names should start with Alfa bytes

Example:
Dim abc 'Correct
Dim a9 'Correct
Dim 7bc 'Incorrect

ii) Cannot contain embedded periods

Dim abc 'Correct declaration
Dim ab c 'Incorrect declaration
Dim ab-c 'Incorrect declaration
Dim ab.c 'Incorrect declaration
Dim ab*c 'Incorrect declaration
Dim ab_c 'Correct declaration

iii) Must not exceed 255 characters

Variable minimum size 1
Variable maximum size 255

iv) Must be unique in the scope of declaration

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

v) Should not use reserved words

Dim For 'For is VBScript Reserved word
-------------------------------------
f) Scope of Variables
 

2 types of Scope for VBScript variables

i) Script Level variables

These can be used for entire script.

ii) Function Level variables

These can be used within the Function only.

Example:

Dim a, b, c 'Script level variables
a =10
b=20
c = a + b '(10 +20) 30
Msgbox c
Function xyz()
    Dim d, e, f 'Function Level variables
    d = 40
    e = 50
    f = a + d '(10+40) 50
    Msgbox f
End Function
Call xyz()
Dim g, h
g=70
h = b + d + g '(20 + 0 + 70) '90
msgbox h '90
--------------------------------------
g) Types of Variables


h) Array variables
   

    i) Constant Arrays, Dynamic Arrays, Dimensional Arrays
    ii) Assign series of values at a time to Array variables
----------------------------------------------

0 comments:

Post a Comment