Collection Class

Collection Class

 

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

Namespace:   Microsoft.VisualBasic
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

System.Object
  Microsoft.VisualBasic.Collection

[<Sealed>]
[<SerializableAttribute>]
type Collection = 
    class
        interface ICollection
        interface IList
        interface ISerializable
        interface IDeserializationCallback
    end

NameDescription
System_CAPS_pubmethodCollection()

Creates and returns a new Visual Basic Collection object.

NameDescription
System_CAPS_pubpropertyCount

Returns an Integer containing the number of elements in a collection. Read-only.

System_CAPS_pubpropertyItem(Int32)

Returns a specific element of a Collection object either by position or by key. Read-only.

System_CAPS_pubpropertyItem(Object)

Returns a specific element of a Collection object either by position or by key. Read-only.

System_CAPS_pubpropertyItem(String)

Returns a specific element of a Collection object either by position or by key. Read-only.

NameDescription
System_CAPS_pubmethodAdd(Object, String, Object, Object)

Adds an element to a Collection object.

System_CAPS_pubmethodClear()

Deletes all elements of a Visual Basic Collection object.

System_CAPS_pubmethodContains(String)

Returns a Boolean value indicating whether a Visual Basic Collection object contains an element with a specific key.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodGetEnumerator()

Returns a reference to an enumerator object, which is used to iterate over a Collection object.

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodRemove(Int32)

Removes an element from a Collection object.

System_CAPS_pubmethodRemove(String)

Removes an element from a Collection object.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_privmethodICollection.CopyTo(Array, Int32)

Copies the elements of the Collection to an Array, starting at a particular Array index. Implements the ICollection interface.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIEnumerable.GetEnumerator()

Returns an enumerator that iterates through the collection. Implements the ICollection interface.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIList.Add(Object)

Adds an item to the Collection object. Implements the IList interface.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIList.Clear()

Removes all items from the Collection object. Implements the IList interface.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIList.Contains(Object)

Determines whether the Collection object contains a specific value. Implements the IList interface.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIList.IndexOf(Object)

Determines the index of a specific item in the Collection object. Implements the IList interface.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIList.Insert(Int32, Object)

Inserts an item to the Collection object at the specified index. Implements the IList interface.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIList.Remove(Object)

Removes the first occurrence of a specific object from the Collection object. Implements the IList interface.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIList.RemoveAt(Int32)

Removes the Collection object item at the specified index. Implements the IList interface.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIDeserializationCallback.OnDeserialization(Object)

Runs after the entire Collection object graph has been deserialized. Implements the IDeserializationCallback interface.

System_CAPS_pubinterfaceSystem_CAPS_privmethodISerializable.GetObjectData(SerializationInfo, StreamingContext)

Returns the data needed to serialize the Collection object. Implements the ISerializable interface.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyICollection.Count

Gets the number of items in this collection. Implements the ICollection interface.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyICollection.IsSynchronized

Gets a value indicating whether access to the Collection object is synchronized (thread safe). Implements the ICollection interface.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyICollection.SyncRoot

Gets an object that can be used to synchronize access to the Collection object. Implements the ICollection interface.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIList.IsFixedSize

Gets a value indicating whether the Collection object has a fixed size. Implements the IList interface.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIList.IsReadOnly

Gets a value indicating whether the Collection object is read-only. Implements the IList interface.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIList.Item(Int32)

Gets or sets the element at the specified index. Implements the IList interface

System_CAPS_noteNote

Whenever possible, you should use the generic collections in the System.Collections.Generic namespace or the System.Collections.Concurrent namespace instead of a Visual Basic Collection. For more information, see Collections (C# and Visual Basic).

The Visual Basic Collection object provides a convenient way to refer to 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 exist 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 of the following:

  • Add an element with the Add method.

  • Remove an element with the Remove method.

  • Remove all elements with 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 entire collection with the For Each...Next Statement (Visual Basic).

    System_CAPS_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.

    System_CAPS_cautionCaution

    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, causing the enumerator to throw an exception. To guarantee thread safety during enumeration, either lock the collection or catch the exceptions resulting from changes made by other threads. For more information on 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, choose 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

.NET Framework
Available since 1.1
Silverlight
Available since 2.0

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show:
© 2016 Microsoft