Solved

Excel: Cleaning up vba classes

Posted on 2014-03-25
4
321 Views
Last Modified: 2014-03-26
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
    4_

   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.

Kelvin4
0
Comment
Question by:Kelvin4
  • 2
  • 2
4 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 39955364
Generally speaking your classes are no different than any other variable as far as VBA is concerned, so if you declare a variable in a procedure as an instance of your class, it will be terminated when the procedure ends.

One thing you do need to watch out for is if your class might end up creating a circular reference (e.g. you have Parent and Child classes and each holds a reference to the other). In these cases, the Parent class should have a cleanup method to explicitly destroy any related children and the code that created the class instance should explicitly call that method. So for your example:

Dim cVisioDataInXLTbls As cVisioDataInXLTbls
   Set cVisioDataInXLTbls = New cVisioDataInXLTbls
   Call cVisioDataInXLTbls.proc1
   Call cVisioDataInXLTbls.CleanUp

Open in new window


Whether you choose to then call:
Set cVisioDataInXLTbls = Nothing

Open in new window


is up to your preference - do you normally do that when you use Range/Worksheet/Workbook variables for example?
0
 

Author Comment

by:Kelvin4
ID: 39955746
Rory:
Thanks for helpful and early advice. I respond to your question...
I look forward to closing this EE question without seeking input from other experts.
----------

In response to your question about my practice re destroying  "Range/Worksheet/Workbook variables"....  I tend to rely on each non-public variable terminating with the scope of the procedure that declares it.

The problem is that although I'm deeply into a long project that is working out well, I lack previous experience to keep me out of trouble-ahead! So:

1. Thanks for the pointer  to parent/child cross references
2. May I conclude from your comments that I need not worry about destroying non-public variables, including Range/Worksheet/Workbook variables?

Cheers,
Kelvin
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 39955932
Yes, you may. I never bother with it (except when using DAO - out of habit I tend to do it then because there used to be a problem with it)
0
 

Author Closing Comment

by:Kelvin4
ID: 39956068
Thanks!
Kelvin
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question