|
||||||
How to Initialise and Terminate a VBScript ClassCreating VBScript Subroutines That Run When a Class is InstantiatedIf a VBScript programmer adds variables to a class, they may need default values.One way to do this is to use subroutines that run when the class is initialized.
When a VBScript programmer decides to create a class then they will add parameters and methods. These parameters and methods are used to model the characteristics that the programmer is trying to achieve. For example, if the programmer were to imagine a class for a person then they may add two key properties:
When the programmer first creates an instance of the class then these will not contain any values. So, if the class definition is: Class Person
public age
public gender
End Class
Then the programmer will will have to set the age of the person themselves: Set Fred = New Person
Fred.age = 21
However, there are two special subroutines that run when a class is initialised and when it is terminated. These are:
Both of these have to be defined as private and so are not available to the user. They are run automatically by the object itself. The Class_Initialize SubroutineIf the programmer requires:
then they will need to add a Class_Initialize subroutine to the class, for example: Private Sub Class_Initialize
age = 21
gender = "unknown"
End Sub
Here the age is set to a default of 21 and gender set to "unknown" so that now the programmer can start using those values immediately: Set Fred = New Person
wscript.echo "Fred is a " & Fred.age & " year old " & Fred.gender
Obviously this will display "Fred is a 21 year old unknown" (as shown in figure 1 at the bottom of this article). The programmer can then go on an change these defaults as required (as shown in figure 2). The Class_Terminate SubroutineIf the Class_Initialize subroutine runs when the object is created then the Class_Terminate subroutine runs when the object is destroyed. It's normally used to clear up any resources used by the object. This can be better understood by looking at a case where two classes are used. The first is a slightly modified version of the class already discussed: Class Person
public age
public gender
public first_name
Private Sub Class_Initialize
first_name = "unknown"
age = 21
gender = "unknown"
End Sub
End Class
This class can then be instantiated by a second: Class Customer
public details
public account_number
Private Sub Class_Initialize
Set details = New Person
End Sub
Private Sub Class_Terminate
wscript.echo details.first_name & " terminated"
Set details = Nothing
End Sub
End Class
In this example the "person" object will be terminated automatically when the script finishes with the customer object. Running the Initialisation and Termination SubroutinesThe key for the programmer is that they now do not have to worry about the resources that the object uses, they just need to make use of their new classes: Set person1 = New Customer
With person1
.details.first_name = "Fred"
.details.age = 30
.details.gender = "male"
End With
Set person1 = Nothing
The end result (as shown in figure 3) is that the programmer can ensure that the object has any necessary defaults set and functionality run when a VBScript object is created and that all memory is correctly released when the object is terminated.
The copyright of the article How to Initialise and Terminate a VBScript Class in Windows Programming is owned by Mark Alexander Bain. Permission to republish How to Initialise and Terminate a VBScript Class in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||