Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a VB6 class have a destructor?

Tags:

class

vb6

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?

like image 247
Brian Hooper Avatar asked Jan 20 '11 15:01

Brian Hooper


People also ask

What is destructor in Visual Basic?

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.

What is a class destructor?

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() .

How many destructors can a class have?

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.

Which method is called when the object is to be destroyed?

A destructor method is called when all references to an object have been destroyed.


1 Answers

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.

like image 60
Heinzi Avatar answered Oct 08 '22 04:10

Heinzi