VBScript Data Types too Variables


VBScript Data Types together with Variables

1) VBScript Data Types 

a) VBScript supports implicit proclamation of Data types
 

> VBScript exclusively Data type is Variant, VBScript variables tin concur whatsoever type of data, based on usage of the information VBScript considers information 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 inwards Computer Programming
 

3 Types of Results

i) Value based Result

Ex:

5 + three = 8

10 * three = 30

ii) Boolean Result

2 means comparing (True /False)

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

3 means comparing (-1/1/0)

Str1 > Str2 together with thence 1
Str1 < Str2 together with thence -1
Str1 = str2 together with thence 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 i sub type to about other using Conversion Functions.
 

Assign values to Variables two types

i) Initialization

Ex:

x = 100

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

Note: Whenever nosotros read information together with thence VBScript considers the information every bit String type data
in guild to perform Mathematical operations together with thence nosotros involve 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 information 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 information to Double sub type
Dim num1, num2
num1 = InputBox("Enter num1 value")
num2 = InputBox("Enter num2 value")
Msgbox Cdbl (num1)  + Cdbl (num2)
-------------------------------------------
'Read information 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 retention place to shop the Data.

Two types of memories inwards Computer Environment

i) Primary retention (RAM)

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

Note: Variables shop inwards Primary memory
---------------------------------
b) Implicit together with explicit Declaration of Variables
 

Using either Public or Private or Dim keywords nosotros tin 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 together with Explicit proclamation of Variables
 

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

Note: Explicit proclamation of Variables is Best practice.

If nosotros purpose implicit variables, user may misspell variables.

Dim Tickets, Price, Total
Tickets = 7
Price = 100
Total = Tickets * Priee
Msgbox Total
-------------------------------
Option Explicit
It forces proclamation of all variables inwards 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 two 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 concur the Data

a = 10 ^ three * vii 'Storing the value that returned past times a program
Msgbox a '7000

a = Now ''Storing the value that returned past times 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 outset amongst Alfa bytes

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

ii) Cannot incorporate 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 non top 255 characters

Variable minimum size 1
Variable maximum size 255

iv) Must live on unique inwards the compass of declaration

Example:
Dim a, b, c
Dim d, e, f
Dim g, h, Influenza A virus subtype H5N1 'Incorrect

v) Should non purpose 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 tin live on used for entire script.

ii) Function Level variables

These tin live on used inside the Function only.

Example:

Dim a, b, c 'Script marking 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 serial of values at a fourth dimension to Array variables
----------------------------------------------

Sumber http://www.gcreddy.com/
Post a Comment (0)
Previous Post Next Post