When I execute a statement such as
Set MyObject = Nothing
is there a particular function inside the class that is invoked (i.e. that I can use as a destructor), to do things like clean up arrays, disconnect from databases, and so forth?
A destructor is a special member Sub of a class that is executed whenever an object of its class goes out of scope. A destructor has the name Finalize and it can neither return a value nor can it take any parameters.
A destructor is a member function that is invoked automatically when the object goes out of scope or is explicitly destroyed by a call to delete . A destructor has the same name as the class, preceded by a tilde ( ~ ). For example, the destructor for class String is declared: ~String() .
It is not possible to define more than one destructor. The destructor is only one way to destroy the object create by constructor. Hence destructor can-not be overloaded.
A destructor method is called when all references to an object have been destroyed.
Analogous to Class_Initialize
, the constructor, there's also a destructor:
Sub Class_Terminate
... ' Put your destructor code here '
End Sub
This method is executed as soon as the reference count of this object reaches zero, i.e., when all variables that reference this object have gone out of scope or have been set to set to something else (e.g. Nothing
). Thus, Set MyObject = Nothing
will only call the destructor if MyObject
is the last variable referencing this object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With