Collection 類別

定義

Visual Basic Collection 是已排序的項目集合,可作為單元來參考。

public ref class Collection sealed : System::Collections::IList
public ref class Collection sealed : System::Collections::IList, System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializable
public sealed class Collection : System.Collections.IList
[System.Serializable]
public sealed class Collection : System.Collections.IList, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
type Collection = class
    interface ICollection
    interface IEnumerable
    interface IList
type Collection = class
    interface ICollection
    interface IList
[<System.Serializable>]
type Collection = class
    interface ICollection
    interface IList
    interface ISerializable
    interface IDeserializationCallback
Public NotInheritable Class Collection
Implements IList
Public NotInheritable Class Collection
Implements IDeserializationCallback, IList, ISerializable
繼承
Collection
屬性
實作

範例

下列範例會 Collection 建立 物件 names 和對話框,讓使用者可以將物件 (名稱) 新增至集合。 然後會顯示集合中的名稱,最後會清空集合,而不需要處置 Collection 物件本身。

若要查看運作方式,請從 [專案] 功能表選擇 [新增類別] 命令,並在 (類型 Public instanceName) 模組層級宣告名為 instanceNamenameClass公用變數,以保存每個實例的名稱。 將預設名稱保留為 nameClass。 將下列程式代碼複製並貼到另一個模組的 [一般 ] 區段中,然後使用另一個程式中的 語句 classNamer 加以啟動。 (此範例只適用於支援 classs.) 的主應用程式

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

備註

注意

可能的話,您應該使用命名空間或命名空間中的System.Collections.Generic泛型集合,而不是 Visual Basic CollectionSystem.Collections.Concurrent 如需詳細資訊,請參閱 集合 - C#集合 - Visual Basic

Visual Basic Collection 物件提供方便的方式,將相關專案群組稱為單一物件。 集合中的專案或 專案只需要與其存在於集合中的事實相關。 集合的專案不需要共用相同的數據類型。

您可以建立集合的方式與建立其他物件的方式相同,如下列範例所示。

Dim coll As New Microsoft.VisualBasic.Collection()  

建立集合之後,您可以執行下列任一動作:

  • 使用 Add 方法新增專案。

  • 使用 Remove 方法移除專案。

  • 使用 Clear 方法移除所有元素。

  • 瞭解集合包含多少元素與 Count 屬性。

  • 檢查是否有特定專案與方法一起 Contains 存在。

  • 使用 Item[] 屬性從集合傳回特定專案。

  • 使用 For Each 逐一查看整個集合 ...下一個語句

    注意

    雖然 Visual Basic Collection 物件的功能與 Collection Visual Basic 6.0 中的物件相同,但兩者無法在 COM 環境中互操作。

    警告

    逐一查看 Visual Basic Collection 不是安全線程的程式。 即使集合已同步處理,其他線程仍可修改集合,導致列舉值擲回例外狀況。 若要保證列舉期間的線程安全,請鎖定集合或攔截其他線程所做的變更所造成的例外狀況。 如需鎖定程式設計項目的詳細資訊,請參閱 SyncLock 語句

建構函式

Collection()

建立並傳回新的Visual Basic Collection 物件。

屬性

Count

取得集合中的元素數目。

Item[Int32]

依位置或索引鍵取得 Collection 物件的特定元素。

Item[Object]

依位置或索引鍵取得 Collection 物件的特定元素。

Item[String]

依位置或索引鍵取得 Collection 物件的特定元素。

方法

Add(Object, String, Object, Object)

將項目新增至 Collection 物件。

Clear()

刪除 Visual Basic Collection 物件的所有項目。

Contains(String)

傳回值,指出 Visual Basic Collection 物件是否包含具有特定機碼的項目。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetEnumerator()

傳回逐一查看集合的列舉值。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Remove(Int32)

Collection 物件移除項目。

Remove(String)

Collection 物件移除項目。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

ICollection.CopyTo(Array, Int32)

從特定的 Collection 索引開始,將 Array 的項目複製到 Array

ICollection.Count

取得此集合中的項目數。

ICollection.IsSynchronized

取得值,該值指出是否同步處理 Collection 物件的存取權 (安全執行緒)。

ICollection.SyncRoot

取得可用來同步處理對 Collection 物件之存取的物件。

IDeserializationCallback.OnDeserialization(Object)

在整個 Collection 物件圖形還原序列化之後執行。

IEnumerable.GetEnumerator()

取得可逐一查看集合的列舉值。

IList.Add(Object)

將項目新增至集合。

IList.Clear()

將所有項目從 Collection 物件中移除。

IList.Contains(Object)

判斷 Collection 物件是否包含特定的值。

IList.IndexOf(Object)

判斷 Collection 物件中特定項目的索引。

IList.Insert(Int32, Object)

將項目插入至指定之索引的 Collection 物件。

IList.IsFixedSize

取得值,指出 Collection 物件是否有固定的大小。

IList.IsReadOnly

取得值,該值指出 Collection 物件是否為。

IList.Item[Int32]

在指定的索引位置上取得或設定項目。

IList.Remove(Object)

Collection 物件中移除第一次出現的特定物件。

IList.RemoveAt(Int32)

移除指定之索引的 Collection 物件項目。

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

傳回序列化 Collection 物件所需的資料。

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於