Login   /   Register

Introduction to classes

Rate this article
     1 votes, average: 4 out of 51 votes, average: 4 out of 51 votes, average: 4 out of 51 votes, average: 4 out of 51 votes, average: 4 out of 5
Loading ... Loading ...
March 29th, 2008 by Yaron Assa

This article will very briefly cover the subject of VBScript classes. My purpose is not to do a detailed sweep, but only a quick reminder in order to move forward to more advanced topics and techniques in articles to come. You can find detailed information and demos on this subject in the book Scripting Quicktest Professional, Advanced VBScript chapter (page 107 and onward). 

Background

Classes allow us to define new objects that are tailored made for our specific needs, thus allowing us to greatly simplify our code and scripts. A single class can bind together data and operations that can be performed on it, and isolated this complex array from other parts of out code.

Due to the massive scope of the subject, I won’t specify the dramatic implications inherent in classes and objects with regards to code design. I’ll just mention that calculated use of classes can upgrade your code readability and simplicity tenfold.

Syntax

A class is define by using the following syntax:

Class ClassName
    Private Sub Class_Initialize  
        'This code will execute automatically when a class object is created
    End Sub
    Private Sub Class_Terminate     
        ' This code will execute automatically when a class object is destroyed
        'e.g. When the class object is set to Nothing.
    End Sub
End Class

Adding the Class_Initialize and Class_Terminate procedures is not mandatory, but they are extremely useful. Notice the reserved keyword Private next to the procedure declaration – it marks the procedure as available only for code hosted with-in this class. This means that you cannot directly call these procedures from outside the class code. In order to create a method / procedure which is callable from "the outside", we must define it as Public:

Class ClassName
    Public Funciton DoSomething
          DoSomething = "This message will return to the outside world"
    End Funciton
End Class

Inside the class, we can even define variables. Like methods and procedures, variables can also be Private – which means they would only be accessible to members of the class, or Public – which means they can be used by the code from outside the class.

Class ClassName
    Public sName
    Private sEMail
 
    ’sEMail can’t be accessed directly, you must use ReturnEMail to get it
    Public Function ReturnEMail
          ReturnEMail = sEMail
    End Function
End Class

Sometimes we would like to protect our class data, and better control the way to access it. For example, we might want the data to be accessible from outside the class, but in read-only mode. Similarly, we may want to perform some operations when the data in read / written (e.g. sort an incoming array). In order to do all these things, we can warp our data with a property structure. A property structure is a collection of mini-procedures, specifying what to do when the data is read / written.

In the following example you can see the use of properties Get, Let and Set structures. Get is used when the data is read, Let when it is written, and Set when it is written with an object reference. As you’ll see in the following example, not all structures are mandatory – the Email property has only a Get structure, which makes it a read-only property.

Class ClassName
    Private sEMail
    Private sName
 
    'The word GET indicates this mini-procedure will be executed when the user reads the property
    Public Property Get Email
         Email = sEMail ‘What value will be returned to the user
    End Property
 
    Public Property Get Name
        Name = sName
    End Property
 
    Public Property Let Name(sNewName)
        sName = sNewName
        If sNewName = "AdvancedQTP" Then MsgBox("This is cool")
    End Property
 
End Class
 

The class object

Classes are different than your everyday VBScript element. For example, while we can use procedures, variables and methods immediately after they’ve been defined, classes are just blueprints, and we must create a class object in order to use them (just like you can’t drive a blueprint of a car – you need to actually build it). A class object is also called an instance of the class. You can create many different instance of a class, and they’ll behave independently of each other:

Class ClassName
    Public Name
    Private ID
End Class
 
Set A = New ClassName
Set B = New ClassName
 
A.Name = "AdvancedQTP"
B.Name = "Nothing"
 
Msgbox A.Name 'Will print out AdvancedQTP
Msgbox B.Name 'Will print out Nothing
 
A.Id = "Something" 'Will Fail - ID is a private property, and can only be used from within A.

However we should note that one class instance can point to another.

Class ClassName
    Public Parent
    Public Name
End Class
 
Set A = New ClassName
Set B = New ClassName
 
A.Name = "AdvancedQTP"
B.Name = "Nothing"
 
Set A.Parent = B
 
Msgbox A.Parent.Name 'Will print out Nothing

It should be noted that while the Let and Set structures seem to get a parameter, their usage in the class instance is not by sending a parameter (e.g. A.Name("AdvancedQTP")), but rather by value assignment (meaning A.Name = "AvancedQTP").

When a class refers to itself, it is sometimes useful to use the reserved keyword Me. The Me keyword is only allowed from within a class instance, and it is used to refer to that instance "from the outside". So Me.PrivateFunction will not work (even though the command is within the class structure, and "sees" PrivateFunction), since Me simulated calling the method "from the outside", and that’s not allowed.

Summery

The subject of classes is wide and deep, and include more syntax options, common tricks, and huge implications on code design and usage. We haven’t even screeched the surface, but as I’ve mentioned, this article was only a brief reminder to lay out the road for articles to come.

If you’d like to read more on the subject, I suggest you Google for further resources and demos, and look from projects which make extensive use of classes such as ReporterManager, or the community project in SQAForums.

In the following weeks, I’ll publish articles that will explore advanced uses of classes, and how they can be structured and managed to create extendable, easy to maintain code.

Posted in Using Classes

6 Responses to “Introduction to classes”

  1. Class Composition | AdvancedQTP Says:

    [+]

    [...] This article deals with classes and their instances in VBScript. Make sure you’re familiar with the subject by reading the... ...

  2. heqingbluesky Says:

    [-]

    This article give us a briefing knowledge on Class, which is helpful for us to do more things on class-based developement.

  3. validuser98 Says:

    [-]

    nice article…i liked it..please post some more interesting materials like this on classes…

  4. An improved dictionary object | AdvancedQTP Says:

    [+]

    [...] be more robust and flexible than the native Scripting.Dictionary. You might want to brush up your VBScript class knowledge b... ...

  5. NEC-sunj Says:

    [-]

    the article is helpful for me ;now i am learing it.thanks a lot

  6. huemach Says:

    [+]

    Seems cannot create class instance directly from QTP's Action Ex: Class abc save at fileabc.vbs At QTP's Action type bellow stat... ...

Leave a Reply

You must be logged in to post a comment.

This article was viewed 1266 times