Closing a reference to a class called within another class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

    #1

    Closing a reference to a class called within another class

    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?
    Code:
    Dim cls as Class1
    Set cls = New Class 1
    ...
    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.
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32669

    #2
    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.

    Comment

    • Seth Schrock
      Recognized Expert Specialist
      • Dec 2010
      • 2965

      #3
      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.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32669

        #4
        Always a pleasure :-)

        Comment

        Working...