26

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.

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
notnot
  • 4,472
  • 12
  • 46
  • 57
  • 1
    For adding variables to the constructor, see [this StackOverflow question][1]. [1]: http://stackoverflow.com/questions/15224113/pass-arguments-to-constructor-in-vba – GregNash Mar 19 '14 at 14:51

5 Answers5

27

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.

Will Rickards
  • 2,776
  • 2
  • 19
  • 25
17

If you are making a class module in VBA, for the constructor, you can use:

Private Sub class_initialize()
....
End Sub

There are no destructors, since VBA is garbage collected. Just make sure to clean up any circular references, and you should avoid any possible memory leaks.

Paul
  • 6,435
  • 4
  • 34
  • 45
9

There exists Class_Terminate which is pretty much the same as destructor.

TechnoS
  • 91
  • 1
  • 3
9

It's been a while since I've used them, but I don't think you can pass parameters into the constructors. I think that was one of the problems I ran into, but I was running into so many issues of how thse classes worked and how I expected them to work that I may be misremembering.

greg
  • 259
  • 1
  • 1
  • More complicated classes will often have "Start"-type functions and a "objectStarted" state variables to accommodate the principle of a parameterised constructor. – Joel Goodwin Jun 25 '09 at 17:49
2

I confirme that class_initialize and class_terminate.

You can check it by writting this TestClass:

Public testVar As Integer

Private Sub class_initialize()
    Debug.Print "Class init"
    testVar = 10
End Sub

Private Sub class_terminate()
    Debug.Print "Class terminate"
End Sub

And write this code in a module :

Sub test()
   Dim myTestClass As New TestClass
   Debug.Print myTestClass.testVar
End Sub

And then you will see logs in Debug window. But with this test, we can see that class_initialize isn't called when you create the instance (with new) but only at the first call of a methode in the instance or getting a var value.

The class_terminate seems to be called at the end of the testModule (If an instance is maked on Thisworkbook object, terminate is "never" called... probably only when the workbook is closed or when Excel is closed)

Elloco
  • 235
  • 2
  • 9