Name
Terminate Event
Syntax
Private Sub Class_Terminate( )
Description
The Terminate event is fired when the last instance of an object or class is removed from memory.
Rules at a Glance
The Terminate event applies to classes defined with the
Class...End Class
construct.Instances of a class are removed from memory by explicitly setting the object variable to
Nothing
or by the object variable going out of scope.If a script ends because of a runtime error, a class’s Terminate event isn’t fired.
Example
The following example shows a typical terminate event in a class object that decrements a global instance counter used to ensure that only a single instance of a particular utility object is created. When the counter reaches 0, the global object reference to the utility object is destroyed.
Private Sub Class_Terminate( ) glbUtilCount = glbUtilCount - 1 If glbUtilCount = 0 then Set goUtils = Nothing End If End Sub
Programming Tips & Gotchas
The Terminate event is designated in code as:
Class_Terminate ' Correct syntax
This contrasts with the documentation, which incorrectly indicates that the event procedure should be designated as:
classname
_Terminate ' Incorrect syntaxwhere
classname
is the name of the class whose Terminate event is being trapped.The Terminate event is also fired when the object variable holding a reference to the last instance of an object is re-referenced in a
Set
statement using theNew
keyword, or is assigned a reference to a new instance of the same type of object is explicity ...
Get VBScript in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.