Visual Basic/Collections
Collection class provides an array-like container more flexible than an array in some ways and less flexible in other ways. More flexible in some ways than Collection is Dictionary class.
Creating a new collection:
Dim Cats As Collection Set Cats = New Collection
Iterating over a collection:
For Each Cat In Cats Rem Do something on Cat Next
Adding an item:
Cats.Add "Item" Cats.Add "Item", "Key" ...
Accessing an item at an index, in a read-only way:
Cats (3) 'The third member of the collection Cats.Item(3) 'An alternative Cats.Item("Key 3") 'Works if an item has a key associated
Overwriting a item at an index:
- There is no straightforward way.
Removing an item:
Collection.Remove Index Collection.Remove HashKey
The size of a collection:
Cats.Count
Testing the emptiness of a collection:
If Cats.Count=0 Then '... End If
Testing the presence of an element in a collection:
MatchFound = False For Each Member In MyCollection If Member = "SoughtValue" Then MatchFound = True Exit For End If Next
Appending one collection to another:
For Each Member In AppendedCollection ExtendedCollection.Add Member Next
Converting a collection to an array:
Dim MyArray ReDim MyArray(MyCollection.Count - 1) Index = 0 For Each Member In MyCollection MyArray(Index) = Member Index = Index + 1 Next