Visual Basic/Collections

From Wikibooks, open books for an open world
Jump to navigation Jump to search

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

Dimensioning and creating a new collection in one line:

  Dim Cats As New Collection

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:

   NewValue = MyCollection(i) + 1
   MyCollection.Add NewValue, Before:=i
   MyCollection.Remove Index:=i + 1

Removing an item:

  Collection.Remove Index
  Collection.Remove HashKey

The size of a collection:

  Cats.Count

Iterating over a collection, read-only:

  For Each Cat In Cats
     Rem Do something on Cat
  Next

Iterating over a collection, read-write:

  'Fill the collection
  Set MyCollection = New Collection
  For i = 1 To 10
    MyCollection.Add i
  Next
  'Increment each item by 1
  For i = 1 To MyCollection.Count
    NewValue = MyCollection(i) + 1
    MyCollection.Add NewValue, Before:=i
    MyCollection.Remove Index:=i + 1
  Next

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

Using a collection as a queue:

  Set Queue = New Collection
  For i = 1 To 10
    Queue.Add i
  Next
  For i = 1 To 5
    Queue.Remove 1 'Remove from the queue
  Next

Using a collection as a stack:

  Set Stack = New Collection
  For i = 1 To 10
    Stack.Add i
  Next
  For i = 1 To 5
    Stack.Remove Stack.Count 'Remove from the stack
  Next

Using a collection as a set:

  ' Using the key of a collection item will do the trick.
  ' The key has to be a string, hence "Str(i)" below.
  ' Note that Str(1) is " 1", with a leading space.
  Set NumberSet = New Collection
  For i = 1 To 10
    NumberSet.Add i, Str(i) 'Value, Key
  Next
  For i = 5 To 15   
    'Add while catching errors resulting from existence of the key
    On Error Resume Next
    NumberSet.Add i, Str(i) 'Value, Key
    On Error GoTo 0
    'End If
  Next
  For i = 14 To 16
    'Remove
    On Error Resume Next 'Catch error if element not present
    NumberSet.Remove Str(i)
    On Error GoTo 0
  Next
  'Membership test
  On Error Resume Next
  NumberSet.Item(Str(50))
  IsMember=Err.Number=0
  On Error GoTo 0
  '
  Set NumberSet2 = New Collection
  For i = 10 To 25
    NumberSet2.Add i, Str(i)
  Next
  'Union
  Set SetUnion = New Collection
  For Each Item In NumberSet
    SetUnion.Add Item, Str(Item) 'Value, Key
  Next
  For Each Item In NumberSet2
    On Error Resume Next
    SetUnion.Add Item, Str(Item) 'Value, Key
    On Error GoTo 0
  Next
  'Intersection
  Set SetIntersection = New Collection
  For Each Item In NumberSet
    On Error Resume Next
    Dummy = NumberSet2(Str(Item))
    If Err.Number = 0 Then
      SetIntersection.Add Item, Str(Item) 'Value, Key
    End If
    On Error GoTo 0
  Next
  'Set difference
  Set SetDifference = New Collection
  For Each Item In NumberSet
    On Error Resume Next
    Dummy = NumberSet2(Str(Item))
    If Err.Number <> 0 Then
      SetDifference.Add Item, Str(Item) 'Value, Key
    End If
    On Error GoTo 0
  Next