Sep
23
2008
--

Implementing Callback in VBScript

Callback is a function which is executed on completion of a registered event. VBScript is not a event driven language, which means we are limited in terms of events for which callback can be implemented.

This article will demonstrate how to implement a callback on finish/terminate event. This finish/terminate event could be one of the following

  • The script finishes/terminates
  • A function finishes/terminates
  • A class finishes/terminates

CallBack implementation

We will create a special CallBack class for this

Class Callback
	'The object which called
	Dim Caller
 
	'Code to be executed during callback
	Dim CallBackCode
 
	'When the class ends execute the callback code
	Sub Class_Terminate()
		Execute CallBackCode
	End Sub
End Class

Executing code when script ends
If we want a Finalize function to be called when the script ends we can use the below code

Dim OC
Set OC = New Callback
OC.CallBackCode = "Call Finalize(0)"
 
Function Finalize(ByVal ExitCode)
	Msgbox "The code end with exit code - "  & ExitCode
End Function
 
Msgbox "Script ends here"

Executing code when a function ends

Function Test()
	Dim OC
	Set OC = New CallBack
	OC.CallBackCode = "Msgbox ""Function Test ended"""
 
	x = 2/0
	Msgbox "Unreachable code"
End Function
 
Call Test()

The advantage of implementing this call back is that they are guaranteed to be executed even in case the script end with a error or the function ends with an error. In QTP we can similarly execute a code at the end of the Test by declaring the variable in one of the associate global libraries.

Note: The approach works on the concept that all variables are destroyed once the scope in which they exist ends. It is important to make sure the CallBack object is always taken in a variable declared (as shown in code “Dim OC”)

Executing the Callback on a Class method

In case we want a class method to be called on a class object we can use the caller property

Class Test
     Function CallMe()
            Msgbox "You called me"
     End Function
End Class
 
Dim oTest
Set oTest = new Test
 
Dim OC
Set OC = New CallBack
Set OC.Caller = oTest
OC.CallBackCode = "Call Caller.CallMe"
Rating: 7.2/10 (6 votes cast)
Written by Tarun Lalwani in: VBScript | Tags: , , , , , , , , , , , ,
Sep
21
2008
0

VBScript Part 2 - Variables

What is variable?
Variable is a named container used to store values

Declaring a variable
There are three possible ways to declare a variable

Dim <VariableName>
Public <VariableName>
Private <VariableName>

Ex –

Dim x, y, z
Dim a
Dim b, c

Variable naming rules
Variable names should following the below rules

  • Must not exceed 255 characters.
  • Must be unique in the scope in which it is declared.
  • Must begin with an alphabetic character.
  • Cannot contain an embedded period.

Though these are the naming conventions specified by Microsoft. But we will be breaking the last 2 rules later in this article
Assigning values to variables
Variables can be assigned values why using the “=” to operator. Below code shows how different types of values can be assigned to a variable

Dim myVar
myVar = "Assigning a string value"
'Assigning a integer value
myVar = 2
'Assigning a double value
myVar = 3.3

Data Type of variables
All variables declared in VBScript are of Data type VARIANT. A VARIANT data type can store any types of value listed below

  • Boolean
  • Byte
  • Date (Time)
  • Double
  • Error
  • Integer
  • Long
  • Object
  • Single
  • String

All actual type of the data stored in a VARIANT type is known as the sub-type of the variable.

Declaring constant variable
Constants can be declared using below syntax

Const  <ConstName> = <ConstValue>

Ex –

Const APP_ NAME = "Testing VBScript"
Const APP_VERSION = "1.01"

It is advised to use all caps variable name when declaring a constant.

Variable Naming conventions
Since variables can store various types of values it is always a good practice to name them with a consistent convention

Data Type

Prefix 1

Prefix 2

Prefix 3

Boolean

b

b_

bln_

Byte

by

by_

byt_

Date (Time)

dt

dt_

dt_

Double

Dbl

dbl_

dbl_

Error

err

err_

err_

Integer

i

i_

int_

Long

l

l_

lng_

Object

o

o_

obj_

Single

sng

sng_

sng_

String

s

s_

str_

Above table shows few prefixes that can be used during scripting. You should choose the ones you like and stick to them. I personally prefer the Prefix 2 column. Below is the list of other Prefixes that might be required

  • Function – fn
  • Sub – sub
  • Global variable – g
  • Class member – m
  • Class – cls

We would be discussing all above mentioned topics in the upcoming articles

Forcing variable declaration
VBScript allows a very poor programming practice which is not to declare the variable being used
Ex –

str_Message = "Tarun Lalwani"
Msgbox str_Message
Msgbox for a undefined variable

Msgbox for a undefined variable

Above code will work even when we haven’t declared the ” str_Message” variable using a Dim/Public/Private statement. VBscript when it looks at a variable name which is not yet declared it declares with an Empty value. In programming a typo when using the variable name is always a possibility. Ex –

str_Message = "Tarun Lalwani"
Msgbox str_Messge
Msgbox for undefined and unused variables

Msgbox for undefined and unused variables

The value above is empty message because we haven’t declared str_Messge variable and when used VBScript declares it with an empty value. Though it is pretty easy to spot the issue in above code but when the code size is huge, these issues become very difficult to debug as wrong variable name used at once place might cause an issue at different location of code. To avoid such a situation VBScript provides an “Option Explicit” statement which can be used at the top of the code as shown below

Option Explicit
str_Message = "Tarun Lalwani"
Msgbox str_Messge

Now above code would throw an error

Undefined variable error when Option explicit is used

Undefined variable error when Option explicit is used

Breaking the 2 rules of naming restriction
Let us reiterate the last 2 rules for variable naming in VBScript

  • Must begin with an alphabetic character.
  • Cannot contain an embedded period.

Thought this is pretty much true the normal way
All below declaration will give syntax errors

Dim 1A
Dim _AB
Dim A B
Dim A.B

Though the error might be different for above declarations but the source of the error is the same, bnot following the naming convention. Now VBScript provides a very less talked about square bracket “[]” declaration

Dim [9X]
Dim [_varName]
Dim [My Name]
Dim [this.A]
[My Name] = "Tarun Lalwani"
[this.A] = [My Name]
MsgBox [this.A]
Msgbox for a undefined variable

Breaking the variable declaration rules

Rating: 8.0/10 (5 votes cast)