473,402 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Destroying A Collection Contained in Another Collection

43
This link mentions a concept that is new to me - Destroying objects by letting a collection hold the reference to it, destroying the original instance and then when done, removing that member of the collection to destroy the object

http://support.microsoft.com/kb/198465

I'm wondering if the same thing is true for collections of collections.

I created a public collection to hold values for forms to set and for queries to use. (I am putting a function that fetches the value in a query calculated field).

Anyway. The collection I created holds sub-collections containing values. Here is a simplified example.

Expand|Select|Wrap|Line Numbers
  1. Public colVals as New Collection
  2. ---------------------------------------
  3. sub test()
  4. Dim col as New Collection
  5.  
  6. ' Add a value to a local collection
  7. col.Add "hi","hi"
  8.  
  9. ' Add this collection to a global collection
  10. colVals.Add col, "col"
  11.  
  12. Set col = Nothing
  13. ' The global collection now owns the reference to the collection
  14.  
  15. End Sub
  16.  
  17. ------------------------------------
  18. Sub test_2()
  19. ' When you run this you can see the sub collection is still persistent
  20.  
  21. MsgBox colGlobalVal.Item("col")("hi")
  22.  
  23. End Sub
  24. ------------------------------------
  25.  
  26. Sub Remove_it()
  27. ' Presumably this destroys the sub collection just as
  28. ' Set col = Nothing
  29. ' would normally destroy it
  30.  
  31. colGlobalVal.Remove ("col")
  32.  
  33. End Sub
  34.  
  35.  
Is it safe to assume that removing the member, as in - "Remove_it" - actually sets the sub-collection ("col" in this example) to Nothing?

I can find no other way to set it to Nothing.
Apr 6 '12 #1
4 1970
I don't see anything in that link that talks about destroying objects. However, take a look at this link, under "Garbage Collection and the Finalize Destructor". It seems that (at least in VB6), the technique used is "Reference Counting", which means that once there are no object variables referencing an object instance, the object instance is destroyed, so in your case, the object should be destroyed once it's removed from the collection.

1. When you create an object instance (assigned to an object variable), the reference count for that instance is one.

2. When you add the object reference of that instance to the collection, the reference count increases to two.

3. Then, when you Set the object variable to Nothing, the reference count decreases to one (so the object instance will NOT be destroyed yet).

4. When you finally remove the object reference from the collection, the reference count decreases to zero, and that is when the object instance is destroyed. Or in the newer garbage-collection systems, the object will now be "orphaned" and will be destroyed "soon".
Apr 7 '12 #2
Sedrick
43
Interesting. I wonder if MS Access works the same way.

Here is the section from that site that I was referring to:
In order to use collections to manage class objects, you must do the following:
  • Create an instance of the class
  • Set the properties and methods of the class
  • Add the class to a public collection
  • Unload the instance of the class
You might expect that unloading the instance of the class results in the class being closed and terminated. However, the class object persists because you add it to a collection, which then owns the reference to the class. This is a very powerful technique that allows you to control object references through a collection; the class object does not terminate until you remove it from the collection.

The following example creates a class object and a form object, and then manages both objects from a collection in a standard module.

I guess they refer to it as "terminate" rather than "destroy", now that I look at it again.

I wonder if "terminate" can be interpreted as set to Nothing.
Apr 7 '12 #3
NeoPa
32,556 Expert Mod 16PB
Sedrick:
I wonder if "terminate" can be interpreted as set to Nothing.
No. I don't believe so, as the concepts are appropriate to different types of objects. This is clouded in VBA as it has Object variables, that other languages may refer to as simply pointers. Setting an object variable to Nothing is simply clearing a pointer. When all pointers are thus cleared the object itself (rather than any pointer) may be cleared away. Do you see the fundamental difference here between an object (an area of memory that maintains various attributes of a conceptual object) and what VBA refers to as an object, which is simply a long-pointer to that same area of memory. It's done to make life easier for those that don't understand the workings, but it makes understanding those workings somewhat more complicated. In VBA an Object variable is not an Object.
Apr 8 '12 #4
Yes, NeoPa's correct. There's a distinction between an object instance and a variable which refers to it. When you declare the variable of a certain object type, you're not declaring that it IS the object, but rather, that it refers to an object of that type. That is why you can use a variant or an object of the generic Object type to hold just about any object (of any type).

Note that when you declare an variable with an object type without using the New keyword, it starts of as just Nothing, and does not refer to any object yet. The New keyword just indicates that the new variable starts of as a entirely New object, e.g.
Expand|Select|Wrap|Line Numbers
  1. Dim r as New Command
In that case, there's both a "new" variable and a new object
It is probably faster but equivalent to:
Expand|Select|Wrap|Line Numbers
  1. Dim r As Command ' Declares r to be able to refer to an object (only) of type Command
  2. Set r=New Command ' Creates a new Command object and places the reference to that new object in r
Setting an variable that refers to an object instance to Nothing will just decrease the reference count, and does not cause VB to free up the memory for that variable unless the reference count becomes zero, so continuing my earlier example,
Expand|Select|Wrap|Line Numbers
  1. Dim r As Command ' Declares r to be able to refer to an object (only) of type Command
  2. Dim r2 As Command
  3. Set r = New Command ' Creates a new Command object and places the reference to that new object in r
  4. r.Name = "NewCommandObject"
  5. Set r2 = r ' There's still only ONE object (the new Command created two lines above), but both r and r2 refers to it
  6. MsgBox r2.Name
  7. Set r = Nothing ' The Command object STILL exists, but now only r2 refers to it
  8. 'MsgBox r.Name ' Causes an error, because r does not REFER to any valid object
  9. MsgBox r2.Name
  10. Set r2 = Nothing ' Cause Access to be able to free up memory for the object instance that r2 refers to
  11.  
Edit: Included an example and changed the object type to Command so that code can actually be tested in Access. You might need to add the reference to the ActiveX Data Objects library.
Apr 9 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Dylan | last post by:
Is there an STL algorithm that will return true if each element in coll1 is present in coll2
1
by: Susanne Bandi | last post by:
Hi, we want to clean and group our packages in collection NULLID. Therefore we rebound (by hand) all the packages from the DB2-Client into a new collection (DB2C) and freed all "old" packages in...
6
by: Jaro | last post by:
hi there, I've problem to create copy of collection of classes. ex.: d1 as mycollection, d2 as mycollection. when I simple set d1=d2 then d1 contain all classes from d2 but their properties are...
9
by: Jeff Mason | last post by:
I'm trying to define a collection which inherits from DictionaryBase. This collection requires a composite key (comprised of two string values) and the value associated with the key is an integer....
11
by: Rob | last post by:
I know, I know, don't use frames. Well, I'm stuck with these frames and I'm trying to add functionality without a complete redsign. You can look at this as a nostalgic journey. Anyway, I've got...
1
by: Øyvind Isaksen | last post by:
I try to make my own ArticleAttribute object and ArticleAttributeCollection, and add data to this Collection. It almost works, but the problem is that each time I add an ArticleAttribute to my...
4
by: Adam Right | last post by:
How can i add a collection to another collection ? For example : --------------------------------------------- StringCollection strColl= new StringCollection(); //string series Hashtable...
2
by: Veloz | last post by:
Hi there My question is regarding how to best return "collections" from a method call. Should you return an actual object or an interesting/ appropriate interface of the object, to the caller?...
36
by: Peter Olcott | last post by:
So far the only way that I found to do this was by making a single global instance of the container class and providing access to the contained class, through this single global instance. Are...
158
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.