How to Initialise and Terminate a VBScript Class

Creating VBScript Subroutines That Run When a Class is Instantiated

© Mark Alexander Bain

Jul 26, 2009
How to Initialise and Terminate a VBScript Class, Mark Alexander Bain
If 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:

  • age
  • gender

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:

  • Class_Initialize
  • Class_Terminate

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 Subroutine

If the programmer requires:

  • properties to be set with default values
  • additional functionality to run when the object is created

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 Subroutine

If 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 Subroutines

The 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.


How to Initialise and Terminate a VBScript Class, Mark Alexander Bain
Figure1: Initialising a VBScript Class, Mark Alexander Bain
Figure 2: Modifying the Default Class Values, Mark Alexander Bain
Figure 3: Terminating a VBScript Class, Mark Alexander Bain
 


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo