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



Sub test()
Dim col As Collection
Dim MyClass As Class1

Set MyClass = New Class1

Set col = New Collection
col.Add MyClass, "A"

MyClass.propNum = 111
Debug.Print col(1).propNum ' 111

col(1).propNum = 222
Debug.Print MyClass.propNum ' 222

End Sub


' code Class1
Dim mNum As Long
Public Property Let propNum(n&)
mNum = n
End Property
Public Property Get propNum&()
propNum = mNum
End Property

In the above MyClass and col(1) both refer to the same instance of the class

Regards,
Peter T

"George Nicholson" <GeorgeNJunk@xxxxxxxxxxx> wrote in message
news:OIJCY#AuHHA.5036@xxxxxxxxxxxxxxxxxxxxxxx
Pretty sure that with custom classes you are adding a
copy of the object to a collection

In my experience, if i create an object from a custom class with a
"StartTime" property, then add that object to 2 different collections,
changing the StartTime of one will not change the StartTime in the other.

My memory is pretty clear on this, but it is not clear on whether there
might have been something else involved. Maybe it was a custom collection
that I hadn't set up properly.


"Peter T" <peter_t@discussions> wrote in message
news:eQlBFR4tHHA.3468@xxxxxxxxxxxxxxxxxxxxxxx
Indeed George, while the MyClass reference exists so does the Class, my
mistake in adjacent post.

Not sure about this bit though -

Pretty sure that with custom classes you are adding a
copy of the object to a collection

Adding the class object to the collection adds a pointer to the sole
class
object, which may also be referenced by other object variables, ?

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

could also do this -

Coll.Add MyClass, "Test"
Set MyClass = Nothing ' or goes out of scope
Coll.Remove 1 'removes item from collection, and destroys the class

Regards,
Peter T

"George Nicholson" <GeorgeNJunk@xxxxxxxxxxx> wrote in message
news:eGdg413tHHA.4424@xxxxxxxxxxxxxxxxxxxxxxx

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" <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: Jscript executing
    ... Hard to tell without seeing the content of Class1 ... Private Sub Form_Load ... Dim MyClass As Class1 ... Set MyClass = New Class1 ...
    (comp.lang.basic.visual.misc)
  • Re: How to terminate an object thats in a collection
    ... Indeed George, while the MyClass reference exists so does the Class, my ... Set MyClass = Nothing 'Removes MyClass from memory ...
    (microsoft.public.excel.programming)
  • Re: How to terminate an object thats in a collection
    ... Coll.Remove 1 'removes item from collection, but MyClass still exists ... Set MyClass = Nothing 'Removes MyClass from memory ... memory outside of the collection. ...
    (microsoft.public.excel.programming)
  • Re: how do you pass string value from a form to class
    ... >no the string value in the from is hard set ... >Dim MyClass As OutAddIn ...
    (microsoft.public.vb.general.discussion)
  • Re: Dataset to Generic.List(Of MyClass)
    ... Dim id As DataColumn = table.Columns ... Dim name As DataColumn = table.Columns ... Dim list As New List(Of MyClass) ... all you're doing is reading data and generating lists of classes, ...
    (microsoft.public.dotnet.framework.aspnet)

Quantcast