Vba – Excel VBA object constructor and destructor


I need to make some custom objects in VBA that will need to reference each other and I have a some issues.

First – how do object constructors work in VBA? Are there constructors?

Second – are there destructors? How does VBA handle the end of the object lifecycle? If I have an object that references others (and this is their only reference), then can I set it to Nothing and be done with it or could that produce memory leaks?

This quasi-OO stuff is just a little bit irritating.

Best Solution

VBA supports Class Modules. They have a Class_Initialize event that is the constructor and a Class_Terminate that is the destructor. You can define properties and methods. I believe VBA uses reference counting for object lifecycle. Which is why you see a lot of Set whatever = Nothing in that type of code. In your example case I think it will not leak any memory. But you need to be careful of circular references.