?
Solved

Excel: Cleaning up vba classes

Posted on 2014-03-25
4
Medium Priority
?
332 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

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 descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

752 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