How to Work With Runtime Objects in VBScript or UFT ?

How to work With VbScript Class
How to work With VbScript Class

How to work with runtime object in VBScript ?

This post talks about runtime objects available in VBscript along with the other topics like: How to Create and destroy objects in UFT? or Step by Step Object Creation process in VBScript. or How to Use With keyword in VbScript?

VB script runtime object are not integral part of VB script interpreter. It is not even part of the VB script language. Runtime objects are known as scripting runtime. These runtime objects are originated from VB or any other com languages. However they are available to VB script during runtime automatically. We should not get confused with the intrinsic objects or built in objects like error debug.

VBScript is different than C.

How to create VBScript Object?

objects need to be created explicitly with the help of

  • Set keyword
  • Reference variable
  • Equals operator
  • Create object command


Option Explicit
Dim myObject
Set myObject= CreateObject( scripting.Dictionary)

Here myObject is the reference variable or the pointer that holds the memory address of the object created.

Set command instructs VBscript engine that the user intend to create an object to the next variable.

Scripting is the library where the objects class definition is stored. Dictionary, filesystem are the name of predefined classes.

READ  What is Windows Script Host (WSH)?

For a few classes like dictionary we can create object using CreateObject( ) method but for few cases like file system object we need to break down the object creation in parts.


Option Explicit
Dim objphyFSO
Dim objFolder
Set objphyFSO= CreateObject("Scripting.FileSystemObject") 
Set objFolder= objphyFSO.getFolder("D:/test/")

Objects provides two features which we can access via code.They are as follows:

  • Properties
  • Methods


A property is a kind of variable( public) that holds value of the object. These values can be simple variant data types including another object.A property can be read only as well.


Similarly a method is just like normal function or procedure that performed some operations on the object. Depending on function it may also return some value.

How to use with keyword

with is an useful keyword while referencing objects. With actually reduces multiple referencing of objects while using objects. That is we can work on series of operations on a specified object without referencing it multiple times.

Option Explicit
Dim myClassVar
Set myClassVar= CreateObject("myclass")
With myClassVar
  .FirstName=" xyz"
  .Email_ID="[email protected]" 
End with 

It is equivalent to

myClassVar.FirstName=" xyz"
myClassVar.Email_ID="[email protected]" 

A more practical Example:

Option Explicit
Dim myobjFSO 
Dim myobjFolder 
Set myobjFSO= CreateObject("scripting.FileSystemObject) 
Set myobjFolder = myobjFSO.GetFolder(("D:\test\") 
With myobjFolder 
msgbox "path" &. Path &VBnewline&_
 "data created" & .DataCreated & VBnewline&_
 "data last accessed" & .DatalastAccessed
End with

General syntax

With [obje variable] or [Object expression]
 = operation on obj variable
End with 

With keyword is a shortcut and can save significant amount of time while working with the object variables. Object variable is a variable that stores a reference to an object. Object expression is an expression that evaluates to an object. With needs an object to work with.

READ  Learn Dictionary Object in UFT or QTP Quickly

Objects with multiple references

Object variables are the references or holders or containers that points to the actual objects.

Advantages of With

  • We do not need to evaluate the object expression every time.
  • We do not need to have temporary variable to hold the resultant object .
  • Use of dot operator can change/ fetch information along with property.
  • More readable, maintainable and understandable.

We can have nested With ..End With

With student 
     With .Name
      End With
    .Email_ID="[email protected]" 
End with

Option Explicit
Dim myobj1 
Dim myobj2
Set myobj1= CreateObject("Scripting.Dictionary")
Set myobj2= CreateObject("Scripting.Dictionary") 
myobj1.Add "why","where" 

Here any myobj1 and myobj2 both are different reference variables created from same class called Scripting.Dictionary.
Hence once we added some value to myobj1 will not impact myobj2. myobj1 contains one element but myobj2 will contain zero element.
However we are free to assign one value to other if they are same type.

you can try the tips and tricks of error handling -described here.

Set myobj2=myobj1 

This will make myobj2 to hold the same object reference as myobj1. As a result it holds same one element.

Object lifetime/ life cycle

It is same as variable lifetime object also has lifetime. How ever for object there are two factors

  1. The variable that holds the reference of the object.
  2. The object itself which stays is in the memory.

When we create an object with CreateObject( ) method ,the object becomes live in the memory. It returns the object reference the variable to acts as a pointer.

Now any point of time if one or more reference variable has/ have reference to the object, the object also said to be alive.

READ  Frequently Asked Coding Questions During Interview on UFT

As soon as the reference count becomes invalid or goes down to zero( no active reference) WSH destroys the object and reclaims the memory.

How the reference count becomes invalid?

An object is ready to be destroyed if one of the following happens

  • The reference variable becomes out of scope.
  • We set a reference variable as nothing.

Nothing is a special keyword in VB script. That instruct VB script engine to destroy the object. Destruction of objects is advisable in a large script as they may lead to errors,unnecessary results or may eat up resources.

General Syntax-


These technique is widely used when open applications using UFT or framework design.

We have two widely used Run time objects. They are

Share and Enjoy !

Leave a Reply

Your email address will not be published. Required fields are marked *