The dictionary object is waaaaay better than a collection! I can do no
better than quote from Ken Getz/Mike Gilbert's Visual Basic Language
Developer's Handbook:
"Why is a Dictionary Better than a Collection?
The Dictionary object fixes several of the glaring errors in the
design of the VBA Collection object -- errors that continue even after
several versions. Perhaps the VBA team doesn't see these issues as
'errors', but they make the Collection object difficult, if not
impossible, to use. In specific,
- You can add items to the dictionary using the Item property. That
is, you can write code like this, to add a new word and initialize its
count in a dictionary named dct:
dct.Item("NewWord") = 1
- You can retrieve both keys and items, given a Dictionary object.
That is, because of the Items and Keys properties, you can retrieve
items from either array. In a collection, you can only retrieve the
items, not the Key values. For example, you might write code like this
to iterate through a Dictionary object, printing out the keys and
values:
Dim i As Integer
For i = 0 to mdct.Count - 1
Debug.Print mdct.Keys(i), mdct.Items(i)
Next i
- You can modify a key once it's been added to the dictionary. For
example, imagine Excel's workbook, a collection of worksheet objects.
Each worksheet must have a unique name within its collection (that is,
the Name property acts as the Key value within its collection), yet
you have always been able to change the Name property of a worksheet.
Using a Collection object, the Key property of an object is write-only
and write-once. If you need to change the Key value, you must delete
the item from the collection and then re-add it with the new key.
Using a dictionary, you can modify the Key property at any time (the
value must continue to be unique within the Dictionary object,
however). For example, to change the Key property from "lowercase" to
"UPPERCASE", you might write code like this:
dct.Key("lowercase") = "UPPERCASE"
From then on, the item associated with the key "lowercase" would now
be associated with the key "UPPERCASE" instead.
- You're not limited to using strings as the Key values for items in a
dictionary. In a collection, each object can either have no Key value,
or a unique string value. In a dictionary, each object must have a
unique Key value associated with it, but that key can be of any data
type.
- A Collection object provides no easy way to determine if a
particular item has already been added to the collection. The
Dictionary object provides the Exists method, which returns True if
the specified Key value already exists within the dictionary.
- A collection provides no obvious way to remove all its items. The
Dictionary class provides a RemoveAll method, effectively resetting
the dictionary."
(end of quoted segment)
I find the Dictionary is far superior. Sure, it might not be
*necessary* to use every single feature of a Dictionary in a specific
application, but I'd rarther standardise on usage across a range of
apps by concentrating only on the Dictionary object.
For another take on the differences between Dictionary and Collection,
see
http://www.nullskull.com/a/740/cool-features-of-visual-basic-60-objects.aspx
MM