Visual Basic/Loops/Collections
Collections in Visual Basic are a built in Object type that in other languages are called Associative Arrays. They combine the properties of one dimensional arrays (they can be indexed) with a unique 'key' that can be also be used to identify an element. Note that if you want to be able to access keys more easily, you may want to use Visual Basic's Dictionary object, explained in the MSDN article, How To Use the Dictionary Object with Visual Basic.
An element in a Collection is actually typed as a Variant, although this fact is transparent when declaring a Collection:
Dim a_collection As Collection
This declares a Collections. A Collection follows all the rules of Objects. The value of a_collection at this point is the special value Nothing. Initialise the Collection like this:
Set a_collection = New Collection
You will now have an empty collection - one with no members. The Collection object implements several methods which allow you to manipulate it. You can add a member thus:
a_collection.Add "Esmerelda"
This member can be referenced by:
Debug.Print a_collection.Item(1)
Which would show Esmerelda. Note that the Item method is the default one for a Collection so you can omit this:
Debug.Print a_collection(1)
Would produce the same result. In this sense Collections are very similar to arrays (in fact they are most similar to Variant arrays).
... need to add information on keys, removing members, testing numbers of members etc and more advanced information of collections of collections and other weird and wonderful things ...
Last modified on 2 November 2010, at 17:22