Share via


HOW TO:建立物件集合 (Visual Basic)

如同使用任何物件,您會宣告變數以持有物件,然後建立集合物件 (Collection Object) 並將其指派為變數。

針對集合物件,您可以使用 Collection 類別或 .NET Framework 集合類別。 特別是,藉由使用 System.Collections.Generic 命名空間的其中一個類別,您可以建立「泛型」(Generic) 集合。 當集合中每個項目的資料型別相同時,泛型集合就相當有用。 透過只允許加入所要的資料型別,泛型集合強制採用「強型別」(Strong Typing)。 如需詳細資訊,請參閱 HOW TO:定義型別安全集合 (Visual Basic)

集合物件一經建立後,您可以在集合中加入和移除項目,並進行項目存取。

以下為兩個如何建立集合的範例。 每個集合都有 String 項目,並將 String 索引鍵與每個項目產生關聯。 前兩個程序使用 Visual Basic 集合類別建立集合, 後兩個程序則使用 .NET Framework 泛型集合類別建立集合。

若要使用 Visual Basic 集合類別建立集合

  1. 宣告並建立 Visual Basic Collection 變數,如下列範例所示。

    Dim sampleVisualBasicColl As New Microsoft.VisualBasic.Collection()
    

    sampleVisualBasicColl 中的集合可以接受任何資料型別的項目。

  2. 使用 Add 方法將項目加入集合中。 下列範例會建立四個 String 項目,並將其加入集合中。 它會建立唯一的 String 值做為每個新項目的索引鍵,並將此值傳遞給 Add 方法。

    Dim item1, item2, item3, item4 As String
    item1 = "Items"
    item2 = "In"
    item3 = "A"
    item4 = "Collection"
    sampleVisualBasicColl.Add(item1, "firstkey")
    sampleVisualBasicColl.Add(item2, "secondkey")
    sampleVisualBasicColl.Add(item3, "thirdkey")
    sampleVisualBasicColl.Add(item4, "fourthkey")
    

    Key 引數在 Visual Basic 集合中是選擇性項目。

  3. 如果要移除集合中的項目,可以使用 Remove 方法,以其位置索引或選擇性的索引鍵來識別項目。 下列範例將說明這點。

    ' Remove the first element of the Visual Basic collection.
    sampleVisualBasicColl.Remove(1)
    ' Remove the element with the key "secondkey".
    sampleVisualBasicColl.Remove("secondkey")
    

    請注意,從 Visual Basic Collection 移除項目時,索引值會透過 Count 屬性的值從 1 進行重新編號。

若要使用 For Each...Next 處理 Visual Basic 集合項目

  1. 宣告儲存於集合中的型別變數。 對於上一個範例,請宣告型別 String 的變數,如下列範例所示。

    ' Insert code from the preceding example.
    Dim aString As String
    
  2. 使用 For Each...Next 陳述式 (Visual Basic) 來檢查集合的每個項目。 下列範例會搜尋特定字串,並於找到時加以顯示。

    For Each aString in sampleVisualBasicColl
        If aString = "Collection" Then
            MsgBox(aString)
        End If
    Next aString
    

若要使用泛型集合類別建立集合

  1. 宣告並建立 .NET Framework System.Collections.Generic.Dictionary<TKey, TValue> 變數,如下列的範例所示。

    Dim sampleGenericColl As New System.Collections.Generic.Dictionary(Of String, String)
    

    sampleGenericColl 變數持有的「型別安全」(Type-Safe) 集合,只能接受型別為 String 的項目和索引鍵。

  2. 使用 Dictionary<TKey, TValue>.Add 方法將項目加入集合中。 下列範例會建立四個 String 項目,並將其加入集合中。 它會建立唯一的 String 值做為每個新項目的索引鍵,並將此值傳遞給 Add 方法。

    Dim item1, item2, item3, item4 As String
    item1 = "Items"
    item2 = "In"
    item3 = "A"
    item4 = "Collection"
    sampleGenericColl.Add("firstkey", item1)
    sampleGenericColl.Add("secondkey", item2)
    sampleGenericColl.Add("thirdkey", item3)
    sampleGenericColl.Add("fourthkey", item4)
    

    在此泛型集合中必須要有 Key 引數。

  3. 若要移除集合中的項目,請使用 IDictionary<TKey, TValue>.Remove 方法。 您必須提供索引鍵以識別要移除的項目。 下列範例將說明這點。

    If Not sampleGenericColl.Remove("thirdkey")
        ' Insert code to handle "thirdkey" not found in collection.
    End If
    

    您可以使用 For Each...Next 陳述式,以迴圈方式檢查集合項目並加以處理,如下列程序所示範。

若要使用 For Each...Next 處理泛型集合項目

  1. 宣告儲存於集合中的型別變數。 對於上一個範例,請宣告型別 String 的變數,如下列範例所示。

    ' Insert code from the preceding example.
    Dim aPair As KeyValuePair(Of String, String)
    
  2. 使用 For Each...Next 陳述式 (Visual Basic) 來檢查集合的每個項目。 下列範例會搜尋特定字串,並於找到時加以顯示。

    For Each aPair In sampleGenericColl
        If aPair.Value = "Items" Then
            MsgBox(aPair.Key & " -- " & aPair.Value)
        End If
    Next aPair
    

請參閱

工作

HOW TO:建立物件的陣列 (Visual Basic)

參考

Collection

System.Collections

System.Collections.Generic

System.Collections.Specialized