How to terminate an object that's in a collection
There does appear to be something particular about removing the last class
object/item in a collection. Although the last item is removed from the
collection as expected, the "last class" it seems is only destroyed when the
routine terminates.
Try commenting/uncommenting the various tests in the following -
Dim col As Collection
Sub test()
Set col = New Collection
Dim cls As Class1, s$
For i = 1 To 3
Set cls = New Class1
cls.sName = "class" & i
col.Add cls, cls.sName
Next
' this triggers the terminate immediately
'col.Remove "class2" ' or by index simply 2
' but this only triggers the terminate when the routine ends
'col.Remove "class3" ' or 3
'For i = 1 To 3
For i = col.Count To 1 Step -1
col.Remove i ' or "class" & i
Next
Debug.Print col.Count ' 0 as expected
'Set col = Nothing
End Sub ' class3 always terminates here
'''' in Class1
Public sName As String
Private Sub Class_Terminate()
Debug.Print "Class_Terminate " & sName
End Sub
Regards,
Peter T
"vivmaha" wrote in message
...
"Coll" is a standard VBA Collection object.
The first item in it is a MyClass object.
How do I delete this object? (and free the memory used)
This does not work:
Coll.remove 1 'The memory of item 1 is still allocated
This crashes:
Set Coll.item(1) = nothing '<Crash occurs here (Err 438)
Coll.remove 1
Thanks.
|