Collection of Objects 
Author Message
 Collection of Objects

I am using a collection to store any number of any different type of
objects... Could be a Word application, Outlook, FSO, or any other COM
componant....  Basically I am using this method

Set Obj = CreateObject("Someobject.someclass")
myCollection.Add Obj
Set Obj = nothing

It works great.

When I want to shut down my application how to I release the references of
each of the individual objects...  I cannot use

Set myCollection(1) = Nothing

and I cant do a similar thing using a for each...

Does setting the collection object to nothing set all of its items to
nothing, or are they just forgotten in memory somewhere?

Any help is appreciated...

Kip Fryman,



Sun, 26 Oct 2003 21:34:13 GMT  
 Collection of Objects
I guess you can use

Set myCollection(1) = Nothing

as Item method is the default method of collections, so the above actually
means

Set myCollection.Item(1) = Nothing.

This way you can release references to all objects.

HTH,
Pooja.


Quote:
> I am using a collection to store any number of any different type of
> objects... Could be a Word application, Outlook, FSO, or any other COM
> componant....  Basically I am using this method

> Set Obj = CreateObject("Someobject.someclass")
> myCollection.Add Obj
> Set Obj = nothing

> It works great.

> When I want to shut down my application how to I release the references of
> each of the individual objects...  I cannot use

> Set myCollection(1) = Nothing

> and I cant do a similar thing using a for each...

> Does setting the collection object to nothing set all of its items to
> nothing, or are they just forgotten in memory somewhere?

> Any help is appreciated...

> Kip Fryman,



Sun, 26 Oct 2003 22:14:18 GMT  
 Collection of Objects
In the MSDN article 'The Visual Basic Collection Object' it says:
<quote>
Like any other object, a Collection object will be destroyed when the
last variable that contains a reference to it is set to Nothing or goes out
of scope. All the object references it contains will be released.
</quote>

Alex


Quote:
> I am using a collection to store any number of any different type of
> objects... Could be a Word application, Outlook, FSO, or any other COM
> componant....  Basically I am using this method

> Set Obj = CreateObject("Someobject.someclass")
> myCollection.Add Obj
> Set Obj = nothing

> It works great.

> When I want to shut down my application how to I release the references of
> each of the individual objects...  I cannot use

> Set myCollection(1) = Nothing

> and I cant do a similar thing using a for each...

> Does setting the collection object to nothing set all of its items to
> nothing, or are they just forgotten in memory somewhere?

> Any help is appreciated...

> Kip Fryman,



Sun, 26 Oct 2003 22:00:57 GMT  
 Collection of Objects
I have tried and 'Set myCollection.Item(1) = Nothing' does not work (Error
438).

But this works:

Set myCollection = Nothing

It might be faster to destroy the collection and re-create it
instead of manually removing everything:

Set myCollection = Nothing
Set myCollection = New Collection

Alex


Quote:
> I guess you can use

> Set myCollection(1) = Nothing

> as Item method is the default method of collections, so the above actually
> means

> Set myCollection.Item(1) = Nothing.

> This way you can release references to all objects.

> HTH,
> Pooja.



> > I am using a collection to store any number of any different type of
> > objects... Could be a Word application, Outlook, FSO, or any other COM
> > componant....  Basically I am using this method

> > Set Obj = CreateObject("Someobject.someclass")
> > myCollection.Add Obj
> > Set Obj = nothing

> > It works great.

> > When I want to shut down my application how to I release the references
of
> > each of the individual objects...  I cannot use

> > Set myCollection(1) = Nothing

> > and I cant do a similar thing using a for each...

> > Does setting the collection object to nothing set all of its items to
> > nothing, or are they just forgotten in memory somewhere?

> > Any help is appreciated...

> > Kip Fryman,



Sun, 26 Oct 2003 22:19:43 GMT  
 Collection of Objects
Cool thanks...


Quote:
> In the MSDN article 'The Visual Basic Collection Object' it says:
> <quote>
> Like any other object, a Collection object will be destroyed when the
> last variable that contains a reference to it is set to Nothing or goes
out
> of scope. All the object references it contains will be released.
> </quote>

> Alex



> > I am using a collection to store any number of any different type of
> > objects... Could be a Word application, Outlook, FSO, or any other COM
> > componant....  Basically I am using this method

> > Set Obj = CreateObject("Someobject.someclass")
> > myCollection.Add Obj
> > Set Obj = nothing

> > It works great.

> > When I want to shut down my application how to I release the references
of
> > each of the individual objects...  I cannot use

> > Set myCollection(1) = Nothing

> > and I cant do a similar thing using a for each...

> > Does setting the collection object to nothing set all of its items to
> > nothing, or are they just forgotten in memory somewhere?

> > Any help is appreciated...

> > Kip Fryman,



Sun, 26 Oct 2003 23:13:26 GMT  
 Collection of Objects
If you guys read my first message you will read that I tried to use

Set myCollection(1) = Nothing but it would not work for some reason... I am
not sure why either..


Quote:
> I have tried and 'Set myCollection.Item(1) = Nothing' does not work (Error
> 438).

> But this works:

> Set myCollection = Nothing

> It might be faster to destroy the collection and re-create it
> instead of manually removing everything:

> Set myCollection = Nothing
> Set myCollection = New Collection

> Alex



> > I guess you can use

> > Set myCollection(1) = Nothing

> > as Item method is the default method of collections, so the above
actually
> > means

> > Set myCollection.Item(1) = Nothing.

> > This way you can release references to all objects.

> > HTH,
> > Pooja.



> > > I am using a collection to store any number of any different type of
> > > objects... Could be a Word application, Outlook, FSO, or any other COM
> > > componant....  Basically I am using this method

> > > Set Obj = CreateObject("Someobject.someclass")
> > > myCollection.Add Obj
> > > Set Obj = nothing

> > > It works great.

> > > When I want to shut down my application how to I release the
references
> of
> > > each of the individual objects...  I cannot use

> > > Set myCollection(1) = Nothing

> > > and I cant do a similar thing using a for each...

> > > Does setting the collection object to nothing set all of its items to
> > > nothing, or are they just forgotten in memory somewhere?

> > > Any help is appreciated...

> > > Kip Fryman,



Sun, 26 Oct 2003 23:13:12 GMT  
 Collection of Objects


Quote:
> If you guys read my first message you will read that I tried to use

> Set myCollection(1) = Nothing but it would not work for some reason... I am
> not sure why either..

You might consider the Collection to be Read Only.

You wanted to alter the contents of one of the elements in the collection.
You can't, you can only remove it, using the Remove method.

For example:

Dim C as New Collection

C.Add 1, "var"
C("var") = 0      'Error 424

LFS



Sun, 26 Oct 2003 23:46:29 GMT  
 Collection of Objects
Why would it be read only? Just the nature of the collection i suppose, and
thus there is no was to indivdually set an object in a collection to
nothing?

I believe that was the error I got when setting the item to nothing.


Quote:


> > If you guys read my first message you will read that I tried to use

> > Set myCollection(1) = Nothing but it would not work for some reason... I
am
> > not sure why either..

> You might consider the Collection to be Read Only.

> You wanted to alter the contents of one of the elements in the collection.
> You can't, you can only remove it, using the Remove method.

> For example:

> Dim C as New Collection

> C.Add 1, "var"
> C("var") = 0      'Error 424

> LFS



Sun, 26 Oct 2003 23:47:43 GMT  
 Collection of Objects


Quote:
>I am using a collection to store any number of any different type of
>objects... Could be a Word application, Outlook, FSO, or any other COM
>componant....  Basically I am using this method

>Set Obj = CreateObject("Someobject.someclass")
>myCollection.Add Obj
>Set Obj = nothing

>It works great.

>When I want to shut down my application how to I release the references of
>each of the individual objects...  I cannot use

>Set myCollection(1) = Nothing

>and I cant do a similar thing using a for each...

>Does setting the collection object to nothing set all of its items to
>nothing, or are they just forgotten in memory somewhere?

It does clean up properly.  To test just create your own object with
Debug.Print "whatever" in the terminate even of your object, then
see if it's destroyed when the collection you add it to is destroyed.

If it makes you feel good you can do:
   Do While myCollection.Count > 0
      myCollection.Remove(1)
   Loop

-Tim



Mon, 27 Oct 2003 00:21:39 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Access Forms collection, App object or Global object of the Parent application

2. collection of objects

3. Collections and objects

4. collection of objects

5. Serialize Collections and Objects

6. How to create collection of objects?

7. Binding a collection of objects to a datagrid

8. Collections Of Objects

9. Linked list or collections of objects?

10. Error Trapping with Collections of Objects

11. Max space using collection of objects

12. Saving Collections of Objects in VB4

 

 
Powered by phpBB® Forum Software