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 Classconstruct.Instances of a class are removed from memory by explicitly setting the object variable to
Nothingor 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 SubProgramming 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
classnameis 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
Setstatement using theNewkeyword, or is assigned a reference to a new instance of the same type of object is explicity ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access