Visual Basic Language Reference  

Collection Object

A Collection object is an ordered set of items that can be referred to as a unit.

Remarks

The Collection object provides a convenient way to refer to a related group of items as a single object. The items, or members, in a collection need only be related by the fact that they exist in the collection. Members of a collection do not have to share the same data type.

You can create a collection the same way you create other objects. For example:

Dim X As New Collection

Once you have created a collection, you can add members with the Add method and remove them with the Remove method. You can return specific members from the collection with the Item method, and iterate through the entire collection with the For Each...Next statement.

Note   Although the Collection object in version 6 and in .NET have identical functionality, they cannot interoperate in a COM environment.

Example

This example creates a Collection object (MyClasses), and then creates a dialog box in which users can add objects to the collection. To see how this works, choose the Class command from the Project menu and declare a public variable called InstanceName at module level of Class1 (type Public InstanceName) to hold the names of each instance. Leave the default name as Class1. Copy and paste the following code into the General section of another module, and then start it with the statement ClassNamer in another procedure. (This example only works with host applications that support classes.)

Public Class Class1
   Public InstanceName As String
End Class

Sub ClassNamer()
   Dim MyClasses As New Collection()   ' Create a Collection object.
   Dim number As Integer   ' Counter for individualizing keys.
   Dim Msg As String   ' Variable to hold prompt string.
   Dim name As String
   Dim oneInst As Class1 
   Dim nameList As String = ""
   Do
      Dim inst As New Class1()   ' Create a new instance of Class1.
      number += 1   ' Increment Num, then get a name.
      Msg = "Please enter a name for this object." & ControlChars.CrLf _
         & "Press Cancel to see names in collection."
      name = InputBox(Msg, "Name the Collection Items")
      Inst.InstanceName = name   ' Put name in object instance.
      ' If user entered name, add it to the collection.
      If inst.InstanceName <> "" Then
         ' Add the named object to the collection.
         MyClasses.Add (inst, number.ToString())
      End If
   Loop Until name = ""

   For Each oneInst In MyClasses   ' Create list of names.
      nameList &= oneInst.InstanceName & ControlChars.CrLf
   Next

   ' Display the list of names in a message box.
   MsgBox (nameList, , "Instance Names In MyClasses Collection")

   Dim count As Integer
   For count = 1 To MyClasses.Count   
      ' Remove name from the collection.
      MyClasses.Remove(1)   
      ' Since collections are reindexed automatically, remove 
      ' the first member on each iteration
   Next
End Sub

See Also

Collection Object Properties, Methods, and Events | For Each...Next Statements