View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
George Nicholson George Nicholson is offline
external usenet poster
 
Posts: 149
Default How to terminate an object that's in a collection


Coll.Add MyClass, "Test"
Coll.Remove 1 'removes item from collection, but MyClass still exists
Set MyClass = Nothing 'Removes MyClass from memory

You are adding an object to the collection, but the object also exists in
memory outside of the collection. Any given object could possibly exist in
multiple collections. (Pretty sure that with custom classes you are adding a
copy of the object to a collection. In my experience, changing a property of
a custom object in one collection will not "update" the same object's
property in another collection.)

Removing an object from a collection won't remove it from memory, unless it
is the last outstanding reference to the object.

Coll.Add MyClass, "Test"
Set MyClass = Nothing 'removes 1st instance from memory (coll.count still
equals 1)
Coll.Remove 1 'removes object from collection and memory

HTH,

"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.