Setting Class Collections & their Contents to Nothing.

C

chatterbox

Hi Everybody

I would like to check something out that I'm a little wooly on. I
have a project, and when finishing, the code sets the collection
objects and their contents to Nothing. My query is this; if there are
two class collections, A and B, and a number of objects {C} which can
belong to A, B, or A and B, is there a proper order for setting to
Nothing A, B, and {C} ? The reason for my wooly headedness is that I
am not entirely certain what constitutes a Parent-Child cross-
referenced association, and therefore what to do in that case. I'm
half certain this isn't such a case, but I don't really know.

If you can offer me some advice, ideally by way of an exmple, I would
appreciate it.

Regards

chatterbox
 
T

Tom Ogilvy

A and B hold references to C. Even when set to nothing, the variable
continues to exist. Setting A or B to nothing does nothing to the things
referenced in its collection. Setting C to nothing does nothing to the
references held within the A and B collections. There is no parent-child
relationship as I understand your description.
 
C

chatterbox

With the uncertainty of any parent-child associations put to rest, I
can close out all of the class objects in the usual way.

Thanks Tom
 
C

Chip Pearson

You can see what actually happens to the objects quite easily with code like
the following:

'''''''''''''''''''''''''''''''''''''''''''''
' In Class1
'''''''''''''''''''''''''''''''''''''''''''''
Public Name As String
Private Sub Class_Terminate()
Debug.Print "Terminate: " & Me.Name
End Sub


'''''''''''''''''''''''''''''''''''''''''''''
' In Module1
'''''''''''''''''''''''''''''''''''''''''''''
Public A As Collection
Public B As Collection
Public C As Class1


Sub AAA()

Set A = Nothing
Set B = Nothing
Set A = New Collection
Set B = New Collection
Dim M As Long
Dim N As Long
For N = 1 To 3
M = M + 1
Set C = New Class1
C.Name = "A-Only " & CStr(N) & ":3"
A.Add C
Next N
For N = 1 To 3
M = M + 1
Set C = New Class1
C.Name = "B-Only " & CStr(N) & ":3"
B.Add C

Next N
For N = 1 To 3
M = M + 1
Set C = New Class1
C.Name = "A-and-B " & CStr(N) & ":3"
A.Add C
B.Add C
Next N

Debug.Print "-----------------------------------"
Debug.Print " Objects Created: " & CStr(M)
Debug.Print "-----------------------------------"
Debug.Print
Debug.Print "-----------------------------------"
Debug.Print "Destroy A"
Debug.Print "-----------------------------------"
N = 0
Set A = Nothing
Debug.Print "-----------------------------------"
Debug.Print "Existing Objects: (" & CStr(B.Count) & " objects)"
Debug.Print "-----------------------------------"
For Each C In B
N = N + 1
Debug.Print CStr(N), C.Name
Next C


End Sub




--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting LLC
www.cpearson.com
(email on the web site)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top