Does a VB6 class have a destructor?

2019-04-07 14:20发布

问题:

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?

回答1:

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.



回答2:

No. VB6 does not provide any mechanism to the programmer to write something explicitly. What a programmer can do is Set MyObject = Nothing and VB will take care of the rest.

UPDATE:

One can use the Class_Terminate to handle this



标签: class vb6