Solved

Excel: Cleaning up vba classes

Posted on 2014-03-25
4
316 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now