Re: How to terminate an object that's in a collection

Tech-Archive recommends: Fix windows errors by optimizing your registry



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" <vivmaha@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:F9730525-3BFF-4A23-A12E-E117F3B425CB@xxxxxxxxxxxxxxxx
"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.


.



Relevant Pages

  • Re: Modeless form with controls added at runtime
    ... placing of the controls, particularly setting the container of the controls. ... Public Sub Show ... Private ActivePage As Class1 ... Dim cls As Class1 ...
    (microsoft.public.vb.general.discussion)
  • Re: Fire events for all comboboxes in my sheet
    ... Private Sub cbo_Change ... Dim cls As Class1 ... event and also from the deactivate event another routine to Set colCBOcls ...
    (microsoft.public.excel.programming)
  • Re: Fire events for all comboboxes in my sheet
    ... Private Sub cbo_Change ... Dim cls As Class1 ... Set colCBOcls = New Collection ...
    (microsoft.public.excel.programming)
  • Re: sundial program
    ... If he wants my QBasic source, ... DECLARE SUB KeyPause ... CLS ... PRINT "Should screens be saved to disk, ...
    (comp.lang.basic.misc)
  • Re: Workable Scatter Plot Data Points
    ... ' code in a class named Class1 ... Private Sub cht_Select(ByVal ElementID As Long, ... Dim clsChtEvents As Class1 ... Dim mnSeries As Long ...
    (microsoft.public.excel.programming)