QTP tutorials & interview questions


Search more on QTP here

VBScript Classes

Class Hello_World
Public Sub Say_Hello(Name)
MsgBox "Hello, " & Name & ", welcome to " & Garden & "."
End Sub

Public Garden
End Class

Dim MyHello_World
Set MyHello_World = New Hello_World
MyHello_World.Garden = "Fountain"
MyHello_World.Say_Hello "Sachin"

Above we have created a class (Hello_World) and an instance (MyHello_World) of that class. VBScript uses the Class...End Class statements to define the contents of the class. The property (Garden) and procedure (Say_Hello) are also declared within the class. Write the whole code written above in notepad, save it as .vbs and run it.

Members within the class can be declared as private and public. Private members are only visible within the class whereas public members are accessible by any code outside of the class. Public is default.

Procedures (Sub or Function) declared Public within the class are methods of the class. Public variables serve as properties of the class.

Property Let will allow code outside of the class to assign a value to a private variable of the class.

Class A

Private name

Public Property Let assign_name(e_Name)
name = e_Name
End Property
End Class

A Property Let procedure must accept at least one argument. This procedure can also handle the process of data validaiton to check some validation e.g if the value you are assigning is more than 5 characters long or not.

Property Get will allow code outside of the class to read the value of a private property variable.

Class A

Private name

Public Property Let assign_name(e_Name)
name = e_Name
End Property

Public Property Get assign_name()
assign_name = name
End Property
End Class

The Property Get procedure does not accept any arguments, however VBScript allows you to add an argument. For this you have to add an additional argument to the property's corresponding Property Let or Property Set procedure because a Property Let/Set procedure must always have exactly one more argument than its corresponding Property Get procedure.

Property Set - This is an object property assignment procedure used to assign the new property value to the private object variable (if the private variable is an object). Below op_sys is an object read-write property.

Class Machine

Private obj_oOS

Public Property Set op_sys(oObj)
Set obj_oOS = oObj
End Property

Public Property Get op_sys( )
Set op_sys = obj_oOS
End Property
End Class

We can make a property Read-Only in two ways:

1)
By writing only a Property Get procedure for the property:- In the absence of a Property Let procedure, code outside of the class cannot write to the employeeName property.

Class employee

Private ename
Public Property Get employeeName()
employeeName = ename
End Property
End Class

2)
By declaring the Property Get procedure as Public and the Property Let procedure as Private:

Class employee

Private ename

Private Property Let employeeName(strName)
ename = strName
End Property

Public Property Get employeeName()
employeeName = ename
End Property
End Class

Class Methods:

When functions or procedures are written inside the class they are called methods.
If a class method is declared as Public then it will be available to code outside or inside the class, and a method that is declared as Private will be available only to code inside the class.

Class welcome

Private ur_name

Public Property Let Name(var_name)
ur_name =var_name
End Property

Public Sub Showwelcome(var_type)
MsgBox Makewelcome(var_type) & ur_name & "."
End Sub

Private Function Makewelcome(var_type)
Select Case var_type
Case "Formal"
Makewelcome = "welcome, "
Case "Informal"
Makewelcome = "Hello there, "
Case "Casual"
Makewelcome = "Hey, "
End Select
End Function

End Class

Dim my_object
Set my_object = New welcome
With my_object

.Name = "sachin"
.Showwelcome "Informal"
.Showwelcome "Formal"
.Showwelcome "Casual"
End With
Set my_object = Nothing


Class Events

Class_Initialize and Class_Terminate are associated with every class that we create. Class_Initialize is fired whenever an object based of a class is instantiated.

e.g
Set objectname = New classname

Class_Initialize event's general format is:

Private Sub Class_Initialize( )
'Initalization code goes here
End Sub

The Class_Terminate event is fired when the object goes out of scope, or when the object is set to Nothing.

Class_Terminate event's general format is:

Private Sub Class_Terminate( )
'Termination code goes here
End Sub


Another example of a class

Below you will see file system objects of VBScript which allows you to access, copy, open, delete (and much more) files on the operating system.

http://msdn2.microsoft.com/en-us/library/aa711216(VS.71).aspx

Things VBScript cant do (got from

http://www.sqaforums.com/showflat.php?Cat=0&Number=348942&page=0&fpart=all&vc=1)

and some class concepts:

To summarize, the VBScript class concept:

1. Does NOT support inheritance. So it is not possible to create a Collie class which inherits characteristics from a Dog class which inherits characteristics from a Mammal class, etc.

2. Does NOT support polymorphism. Kind of a related to inheritance, where for example a Dog class will “bark” and a Pig class will “oink” when they are asked to “speak” (in a class hierarchy where they inherited the base Speak() method from the Mammal class, then override that method to implement animal unique behavior).

3. DOES support encapsulation, an OO technique which conceals how a particular class is implemented, independent from objects that use the class’s Public Properties and Methods. Another way to think about encapsulation is to say it is the ability to hide implementation details while sharing higher level behavior.
It is this author’s opinion that the lack of inheritance and polymorphism are not major shortcomings in scripting environments such as WSH and especially QTP, where you are not trying to build large complex OO programs.

Encapsulation is then the primary reason to consider using VBScript classes. And, with encapsulation comes namespace control--which permits any number of class elements to be named foo as long as each of those elements resides in a different class (i.e. a different namespace).