Terminating Dependent Classes

I have four objects that are all dependent on each other. They are CInvoice has many CInvoiceLines, CInvoiceLine has many CRoyaltyLines, CRoyaltyLine has many CRoyaltyAdjustments. In order to properly terminate all of these class modules, I need to remove any and all dependencies. If I don’t, the classes will stay alive and consume memory. With four levels like this eating memory, I will eventually have to answer the question: Do I want to send an error report?
I think I have my termination sequences right, but I’ll let you be the judge in the comments. I start in the CInvoice class, which I’ll call a Parent class. It has many children, including the CInvoiceLines class (a child collection) and each individual CInvoiceLine class (a child class). The CInvoiceLine class then becomes a parent class for my next round of terminations. His children are the CRoyaltyLines class and each instance of the CRoyaltyLine class.
All of my Terminate events are structured like
End Sub
I don’t use the built-in class terminate event because it doesn’t fire at the right time. When I’m in a parent class, I do these three things:
- Call the Terminate method of the Child Collection Class
- Set the local Child Collection Class variable to Nothing
- Set the local Parent variable to Nothing
Items 1 and 2 are done in a parent class that is not also a child class. Item 3 is done in a child class that is not also a parent class. All three items are done in a class that is both a parent and a child. For instance, CInoviceLine is a parent with respect to CRoyaltyLines, but a child with respect to CInvoice, so all three steps must occur.
When I’m in a Child Collection Class (ex: CInvoiceLines), I do these three things:
- Call the Terminate method for each member of the collection
- Set the local Parent variable to Nothing
- Set the local Collection variable to Nothing
When item 1 is executed, it may be terminating a class that’s a parent class and the whole things starts over again. Here’s an example: In the CInvoice class
mobjLines.Terminate ‘Term the child collection class
Set mobjLines = Nothing ‘term the local ccc variable
End Sub
Since mobjLines is a CInvoiceLines object, that Terminate method gets called first. In the CInvoiceLines class
Dim i As Long
‘Terminate each member
For i = 1 To mcolLines.Count
mcolLines.Item(i).Terminate
Next i
Set mobjParent = Nothing ‘kill the parent variable
Set mcolLines = Nothing ‘kill the collection variable
End Sub
When
is called, it’s calling the Terminate method of a class that’s both a child and a parent. In CInvoiceLine
mobjRoyaltyLines.Terminate ‘Term the child collection class
Set mobjParent = Nothing ‘kill the parent variable
Set mobjRoyaltyLines = Nothing ‘kill the ccc variable
End Sub
The only difference between this and CInvoice is that I killed the parent variable because it’s also a child class. I won’t go through the rest, except to show you the last class, CRoyaltyAdjustment
Set mobjParent = Nothing
End Sub
Since this is a child class, but not a parent class, only killing the local parent variable is necessary.
Boy, there’s nothing more thrilling than terminating classes, is there? That’s why I added the image – to spice it up a little. For a little background, I’m abstracting my relational database into objects in VBA. That way I can reference
, which I contend is easier to code and read. But the setup is a real pain.








