
' Constructor:
Public Function [new CustomClass]()
Set [new CustomClass] = new cls_CustomClass
End Function
' Class definition:
Class cls_CustomClass
Private Sub Class_Initialize()
Print "Custom Class: I am created!"
End Sub
End Class
' using the object in another library:
Dim myClass
Set myClass = [new CustomClass]
This seems like a drawback, but it creates a great possibility. Normally, it is not possible to pass initialisation parameters into a new class, because Class_Initialize does not accept parameters. But using the constructor function, we can!
Option Explicit
' Make a private variable that we can pass into the class
Private PARAMETER_ARRAY
' Create a function that returns the requested class. Accepts
' a parameter array. Note: Do not use paramArray, it is reserved!
Public Function [new customClass](ByVal parameterArray)
PARAMETER_ARRAY = parameterArray
' Return a clsCustomClass object
Set [new CustomClass] = new cls_CustomClass
End Function
' Create the custom class. Classes are always declared Private in QTP
Class cls_CustomClass
' The sub Class_Initialize is used to initialize the object with the
' parameters passed into the constructor
Private Sub Class_Initialize()
' Do something with the parameters
Print join(PARAMETER_ARRAY, vbNewLine)
End Sub
End Class
' Test code
Dim myClass
Set myClass = [new customClass](array("first parameter", "second parameter", "etc."))
Notice what we just did: We created a Private variable, that can only be used by CustomClass classes. So with the use of Private variables with local scope to the library where you put the class in, you create a static variable for the class!
A static variable in this context is a variable that keeps its value and can be used over multiple objects of the same class:
' Declaring the static variables
Private INSTANCE_COUNTER : INSTANCE_COUNTER = 0
' Constructor:
Public Function [new CustomClass]()
Set [new CustomClass] = new cls_CustomClass
End Function
' Class definition:
Class cls_CustomClass
Private Sub Class_Initialize()
Print "I am created and I have " & INSTANCE_COUNTER & " sister(s)."
INSTANCE_COUNTER = INSTANCE_COUNTER + 1
End Sub
Private Sub Class_Terminate()
INSTANCE_COUNTER = INSTANCE_COUNTER – 1
End Sub
End Class
' using the object in another library:
Dim a, b, c
Set a = [new CustomClass]
Set b = [new CustomClass]
Set c = [new CustomClass]
Results in:
I am created and I have 0 sister(s).
I am created and I have 1 sister(s).
I am created and I have 2 sister(s).
By using the static variable as a reference for the object itself, we can create a singleton object. A singleton is an object whereof only one instance at a time can exist. In object oriented languages, singletons are normally used for large objects, or objects that will go bad if multiple instances exists like deadlocks or instability.
Option explicit
Private SINGLETON : Set SINGLETON = Nothing
Public function [new Singleton]()
If SINGLETON Is Nothing Then
set SINGLETON = new cls_Singleton
End If
Set [new Singleton] = SINGLETON
End Function
Class cls_Singleton
Private Sub Class_Initialize
Print "I am unique!"
End Sub
End Class
Dim st1, st2, st3
Set st1 = [new Singleton]
Set st2 = [new Singleton]
Set st3 = [new Singleton]
Results in only one:
I am unique!
There is a drawback: Once a singleton object is created this way, it is not possible to destroy it without the use of some code violating the object oriented principle.