Solved

Excel: Cleaning up vba classes

Posted on 2014-03-25
4
313 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks!
Kelvin
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

772 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

15 Experts available now in Live!

Get 1:1 Help Now