次の方法で共有


Collection Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Inheritance Hierarchy

System.Object
  Microsoft.VisualBasic.Collection

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

Syntax

'Declaration
<DebuggerDisplayAttribute("Count = {Count}")> _
<DefaultMemberAttribute("Item")> _
Public NotInheritable Class Collection _
    Implements IEnumerable, ICollection, IList
[DebuggerDisplayAttribute("Count = {Count}")]
[DefaultMemberAttribute("Item")]
public sealed class Collection : IEnumerable, 
    ICollection, IList

The Collection type exposes the following members.

Constructors

  Name Description
Public method Collection Creates and returns a new Visual Basic Collection object.

Top

Properties

  Name Description
Public property Count Returns an Integer containing the number of elements in a collection. Read-only.
Public property Item[Int32] Returns a specific element of a Collection object either by position or by key. Read-only.
Public property Item[Object] Returns a specific element of a Collection object either by position or by key. Read-only.
Public property Item[String] Returns a specific element of a Collection object either by position or by key. Read-only.

Top

Methods

  Name Description
Public method Add Adds an element to a Collection object.
Public method Clear Deletes all elements of a Visual Basic Collection object.
Public method Contains Returns a Boolean value indicating whether a Visual Basic Collection object contains an element with a specific key.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public method GetEnumerator Returns a reference to an enumerator object, which is used to iterate over a Collection object.
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Remove(Int32) Removes an element from a Collection object.
Public method Remove(String) Removes an element from a Collection object.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Extension Methods

  Name Description
Public Extension Method AsQueryable Converts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension Method Cast<TResult> Converts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension Method OfType<TResult> Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)

Top

Explicit Interface Implementations

  Name Description
Explicit interface implemetationPrivate method ICollection.CopyTo Copies the elements of the Collection to an Array, starting at a particular Array index. Implements the ICollection interface.
Explicit interface implemetationPrivate property ICollection.Count Gets the number of items in this collection. Implements the ICollection interface.
Explicit interface implemetationPrivate property ICollection.IsSynchronized Gets a value indicating whether access to the Collection object is synchronized (thread safe). Implements the ICollection interface.
Explicit interface implemetationPrivate property ICollection.SyncRoot Gets an object that can be used to synchronize access to the Collection object. Implements the ICollection interface.
Explicit interface implemetationPrivate method IEnumerable.GetEnumerator Returns an enumerator that iterates through the collection. Implements the ICollection interface.
Explicit interface implemetationPrivate method IList.Add Adds an item to the Collection object. Implements the IList interface.
Explicit interface implemetationPrivate method IList.Clear Removes all items from the Collection object. Implements the IList interface.
Explicit interface implemetationPrivate method IList.Contains Determines whether the Collection object contains a specific value. Implements the IList interface.
Explicit interface implemetationPrivate method IList.IndexOf Determines the index of a specific item in the Collection object. Implements the IList interface.
Explicit interface implemetationPrivate method IList.Insert Inserts an item to the Collection object at the specified index. Implements the IList interface.
Explicit interface implemetationPrivate property IList.IsFixedSize Gets a value indicating whether the Collection object has a fixed size. Implements the IList interface.
Explicit interface implemetationPrivate property IList.IsReadOnly Gets a value indicating whether the Collection object is read-only. Implements the IList interface.
Explicit interface implemetationPrivate property IList.Item Gets or sets the element at the specified index. Implements the IList interface
Explicit interface implemetationPrivate method IList.Remove Removes the first occurrence of a specific object from the Collection object. Implements the IList interface.
Explicit interface implemetationPrivate method IList.RemoveAt Removes the Collection object item at the specified index. Implements the IList interface.

Top

Remarks

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 by using the Add method.

  • Remove an element by using the Remove method.

  • Remove all elements by using the Clear method.

  • Find out how many elements the collection contains by using the Count property.

  • Check whether a specific element is present by using the Contains method.

  • Return a specific element from the collection by using the Item property.

  • Iterate through the entire collection by using a For Each...Next loop.

    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, 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 about locking a programming element, see the Visual Basic documentation about the SyncLock statement. 

Examples

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
    name = "SampleCollectionItems"
    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
  msg = nameList
  ' 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

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

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

See Also

Reference