Solved

Excel: Cleaning up vba classes

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

773 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