DWUTKA at marlow.com
DWUTKA at marlow.com
Tue Jun 27 13:39:32 CDT 2006
What is a collection? A collection is just what it sounds like, it represents a group of objects. The mechanics of it are relatively simple too. A collection stores pointers to an object. A point is a Long Integer that represents the location in memory where an object is stored. I know this is an advanced tutorial, but please no shouting about minute details. The important part is that a collection doesn't really 'hold' an object, just a pointer to it. This is important because physically, you can't put one object into multiple containers. But an object in code can be placed in multiple collections. We did this in our demo already. We had two collections, one sorted by first name, one sorted by last name. There was only one real set of Person classes, but their pointers existed in both collections. This is important for several reasons. First of all, you typically don't want duplicate objects floating around. There are cases where you might, but in our example, when we change someone's name, that Person object is changed, and it doesn't matter if we reference it from the first name or last name collection, it will have the new name. If we populated both collections with separate objects, changing one of the objects would not mirror the change in the other collection. Secondly, this applies to the OOP model too. A class object represents something, in this case a person. When a change occurs to the person, it should be reflected everywhere, otherwise we are dealing with a clone, which we all know is not legal. <grin> So in our demo, we created the instances, added them to the collection sorted by first name, then when building the collection sorted by last name, we didn't create new objects, instead, we referenced those existing in the first collection. So, how do we reference objects in a collection? There are a few ways. First, we can reference the item by either it's position, or by it's index. Collections are 1 based, which means that the first object in a collection is in position 1. Example: Dim j as long Dim obj As SomeObject For j=1 to SomeCollection.Count Set obj=SomeCollection(j) Debug.print obj.SomeProperty Next j The index is a non-numeric reference. In our demo, we set the index in our collections to "ID:" & the id of the person. Since the ID of the person is unique, the indexes in the collections are unique (which they have to be). Obviously this would make it difficult to loop through the objects in a collection, but it does make it easy to grab a specific object.: Set obj=SomeCollection("AUniqueIdentifier") It is important to note that positions are numeric, and indexes are not. There is a difference between 1 and "1". I mention this from issues I have run into when developing in ASP, which uses variants, to reference an item's position, I've had to force the values to an integer. (CLng()). With referencing by position, a collection is just like an Array. Referencing by an index is something an Array can't do. And beyond that, we can also reference with a for each loop. Dim obj as SomeObject For Each obj in SomeCollection Debug.print obj.SomeProperty Next Pretty handy, eh? So now that we have a better understanding of collections, in the next part of the tutorial, let's see about expanding on the capabilities of our custom collection. Drew