By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,503 Members | 2,788 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,503 IT Pros & Developers. It's quick & easy.

Closing a reference to a class called within another class

Seth Schrock
Expert 2.5K+
P: 2,931
I have a form that calls a class (class 1). Inside class 1, there is a call to another class (class 2). Normally, I like to set all variables to nothing that I have set to something. My question is, if I run code like the following, is class 2 also removed from memory?
Expand|Select|Wrap|Line Numbers
  1. Dim cls as Class1
  2. Set cls = New Class 1
  3. ...
  4. Set cls = nothing
Or do I need to create a Terminate sub within class 1 that would set class 2 to nothing within class 1 before I set class 1 to nothing? I know that it doesn't hurt anything to do as I just described, but if I don't need to, then there isn't any point in doing it. And I don't know how to test this out for myself.
Nov 7 '13 #1

✓ answered by NeoPa

If the only reference to (that particular instance of) Class 2 is found in your Class 1 instance then I would expect recent versions of VBA to handle tidying that up for you.

It is never recommended to rely on such behaviour however. Some classes deal with items beyond the scope of the VBA code itself. Losing access to such things may just tidy up the code, but leave non-code-based objects around as orphans. It's rare, but you'll never suffer from such a (very difficult to discover and fix) problem if your code is tidy and handles everything logically.

Share this Question
Share on Google+
3 Replies


NeoPa
Expert Mod 15k+
P: 31,186
If the only reference to (that particular instance of) Class 2 is found in your Class 1 instance then I would expect recent versions of VBA to handle tidying that up for you.

It is never recommended to rely on such behaviour however. Some classes deal with items beyond the scope of the VBA code itself. Losing access to such things may just tidy up the code, but leave non-code-based objects around as orphans. It's rare, but you'll never suffer from such a (very difficult to discover and fix) problem if your code is tidy and handles everything logically.
Nov 7 '13 #2

Seth Schrock
Expert 2.5K+
P: 2,931
Okay. That is what I was looking for. I will add a Terminate sub that I can call from the form to terminate the reference to class 2 in class 1.

Thanks NeoPa.
Nov 7 '13 #3

NeoPa
Expert Mod 15k+
P: 31,186
Always a pleasure :-)
Nov 7 '13 #4

Post your reply

Sign in to post your reply or Sign up for a free account.