共用方式為


Contains 方法 (Collection 物件)

更新:2007 年 11 月

傳回 Boolean 值,表示 Visual Basic Collection 物件是否包含具有特定索引鍵的項目。

Public Function Contains( _
    ByVal Key As String _
) As Boolean

參數

  • Key
    必要項。String 運算式,可指定要搜尋集合項目的索引鍵。

例外狀況/錯誤代碼

例外狀況類型

錯誤代碼

條件

ArgumentException

5

指定的 Key 為 Nothing。

如果將使用非結構化錯誤處理的 Visual Basic 6.0 應用程式升級,請參閱「錯誤代碼」資料行 (您可以將錯誤代碼與 Number 屬性 (Err 物件) 比對)。但是,請盡可能考慮以 Visual Basic 的結構化例外處理概觀 取代這類錯誤控制項。

備註

如果集合所包含的項目具有與 Key 完全相符的索引鍵,則 Contains 會傳回 True。否則,Contains 會傳回 False。

Visual Basic Collection 可保留一些具有索引鍵的項目,以及其他不具索引鍵的項目。這視 Add 方法 (集合物件) 的呼叫是否將引數提供給選擇性 Key 參數而定。

範例

Dim customers As New Microsoft.VisualBasic.Collection()
Dim accountNumber As String = "1234"
' Insert code that obtains new customer objects.
' Use the new customer's account number as the key.
customers.Add(newCustomer, accountNumber)
' The preceding statements can be repeated for several customers.
Dim searchNumber As String = "1234"
' Insert code to obtain an account number to search for.
If customers.Contains(searchNumber) Then
    MsgBox("The desired customer is in the collection.")
Else
    MsgBox("The desired customer is not in the collection.")
End If

如果您想要搜尋集合以找到使用其索引鍵的項目,請記得在每次呼叫 Add 方法時提供 Key 引數。

需求

命名空間 (Namespace)︰Microsoft.VisualBasic

**模組︰**Collection

組件 (Assembly):Visual Basic Runtime Library (在 Microsoft.VisualBasic.dll 中)

請參閱

參考

Collection 物件 (Visual Basic)

Item 屬性 (Collection 物件)

Count 屬性 (Collection 物件)

Add 方法 (集合物件)

Remove 方法 (Collection 物件)

Clear 方法 (Collection 物件)