When should I clean up instances of classes?
I have started to use vba classes to:
- respond WithEvents to control selections in UserForms
- encapsulate data processing routines, and write results to new sheets etc.
For UserForm events, the instance of a withEvents class is initialised when the user form is initiated. So do I clear the class instance just before the form closes (always provided that the class does not specify a variable - eg a collection - that might be required outside the life-span of the form)?
For data processing routines, lines 1-3 (below) come from a standard module, in which I initiate an instance of class: cVisioDataInXLTbls and initiate class sub "proc1". Again, no class variable is required to be stored in memory when proc1 is exited.
1_ Dim cVisioDataInXLTbls As cVisioDataInXLTbls
2_ Set cVisioDataInXLTbls = New cVisioDataInXLTbls
3_ Call cVisioDataInXLTbls.proc1
So: do I add code to blank line #4 (above) to clean up cVisioDataInXLTbls?
- What code should I write?
Or: At the end of the class procedure .proc1 (above) do I add:
set me = Nothing.
Else: I notice "class_initialize" and "class_terminate" - should these be in play here?
Again, these classes are transitory; they do not store data in class variables.
Am striving for a firmer grasp, and some explanation would be appreciated.