Thursday, January 3, 2019


Classes in VBScript

Here is an example that shows how we can create a class in QTP. Once we define the class, we can create its objects and then access its method and properties.


'declare the class book.

Class Book

dim bn,bp
'class has 2 variable members bn and bp.

public Property Get bookname()
    ' Get bookname property gets the book name of the object of Book
  bookname = bn
End Property

public Property Let  bookname(x)
  bn = x
   ' Let  bookname property assigns value to the book name of the object of Book
End Property


public Property Get price()
price = bp
End Property

public Property Let  price(x)
bp = x
End Property


function  discountedPrice()
print bp-20
'We can have functions and procedures inside class to process memeber variables
End function


Private Sub Class_Initialize   ' Setup Initialize event.

MsgBox("Object of Book Class created")

End Sub

Private Sub Class_Terminate   ' Setup Terminate event.

MsgBox("Object of Book Class destroyed")

End Sub


End Class

Set b1 = new  Book
'createing the object b2 of the class Book.

b1.bookname = "QTP Tutorials"
b1.price = 220
'assigning value to the object b1

print b1.bookname()
'getting the value of the property bookname.

'accessing the function in class
b1.discountedPrice()

Set b1 = nothing


QTP does support object oriented programming to some extent. We can create constructors and destructors in vbscript classes using class_initialize and class_terminate methods. class_initialize method acts like constructor function which gets called automatically when we create an object of the class. This is how we can create and use the classes in QTP.

Related Posts:

  • Date time functions in vb script Date time functions in vb script It is very important that you know how to work with date and time in VBScript as most of the VBScript programs will have date and time involved in it.Below is the list of all date and … Read More
  • Classes in VBScript Classes in VBScript Here is an example that shows how we can create a class in QTP. Once we define the class, we can create its objects and then access its method and properties. 'declare the class book. Class Bo… Read More
  • How we can define array in vb script How we can define array in vb script Arrays are used to store multiple values in the same variable.We can define array in vb script in 4 different ways. dim a(10)  - static array of 11 elements dim b()  … Read More
  • Error handling in VBScript Error handling in VBScript VBScript provides below statements and keywords to handle errors. on error resume next Err object - err.description, err.number Sample code to handle the error is given below If er… Read More
  • Types of Array in vb script Types of Array in vb script There are 2 types of arrays in vb script. Static(Fixed Size) Dynamic Static ArraysStatic array can contain fixed number of elements in array.Example -dim a (10) - This static array will … Read More

0 comments:

Post a Comment

Blog Archive

Translate

Popular Posts

Total Pageviews

150,592

Blog Archive