We recommend using Visual Studio 2017
This documentation is archived and is not being maintained.

Collection Object (Visual Basic)

Visual Studio 2008

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

The Visual Basic Collection object provides a convenient way to see a related group of items as a single object. The items, or elements, in a collection need only be related by the fact that they are located in the collection. Elements of a collection do not have to share the same data type.

You can create a collection the same way you create other objects, as the following example illustrates.

Dim coll As New Microsoft.VisualBasic.Collection()

Once you have created a collection, you can do any one of the following:

  • Add an element that uses the Add Method.

  • Remove an element that uses the Remove Method.

  • Remove all elements that use the Clear Method.

  • Find out how many elements the collection contains with the Count Property.

  • Check whether a specific element is present with the Contains Method.

  • Return a specific element from the collection with the Item Property.

  • Iterate through the complete collection with the For Each...Next Statement (Visual Basic).

    NoteNote:

    Although the Visual Basic Collection object has functionality identical to the Collection object in Visual Basic 6.0, the two cannot interoperate in a COM environment.

    Caution noteCaution:

    Iterating through a Visual Basic Collection is not a thread-safe procedure. Even if the collection is synchronized, other threads can still modify the collection. This causes the enumerator to throw an exception. To guarantee thread safety during enumeration, either lock the collection or catch the exceptions that results from changes that were made by other threads. For more information about locking a programming element, see SyncLock Statement.

The following example creates the Collection object names and a dialog box with which a user can add objects (names) to the collection. It then displays the names in the collection, and finally empties the collection without disposing of the Collection object itself.

To see how this works, select the Add Class command from the Project menu and declare a public variable called instanceName at the module level of nameClass (type Public instanceName) to hold the names of each instance. Leave the default name as nameClass. 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 works only with host applications that support classes.)

Public Class nameClass
    Public instanceName As String 
End Class 
Sub classNamer()
    ' Create a Visual Basic Collection object. 
    Dim names As New Microsoft.VisualBasic.Collection()
    Dim key As Integer 
    Dim msg As String 
    Dim name As String 
    Dim nameList As String = "" 
    ' 1. Get names from the user to add to the collection. 
    Do 
        Dim inst As New nameClass()
        key += 1
        msg = "Please enter a name for this object." & vbCrLf _
            & "Press Cancel to see names in collection."
        name = InputBox(msg, "Name the Collection items")
        inst.instanceName = name
        ' If user entered a name, add it to the collection. 
        If inst.instanceName <> "" Then
            names.Add(inst, CStr(key))
        End If 
    Loop Until name = "" 
    ' 2. Create and display a list of names from the collection. 
    For Each oneInst As nameClass In names
        nameList &= oneInst.instanceName & vbCrLf
    Next oneInst
    MsgBox(nameList, , "Instance Names in names Collection")
    ' 3. Remove elements from the collection without disposing of the collection. 
    For count As Integer = 1 To names.Count
        names.Remove(1)
        ' Since Visual Basic collections are reindexed automatically,  
        ' remove the first member on each iteration. 
    Next count
End Sub

Namespace: Microsoft.VisualBasic

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

Show: