Collection.Contains(String) 方法


傳回值,指出 Visual Basic Collection 物件是否包含具有特定機碼的項目。Returns a value that indicates whether a Visual Basic Collection object contains an element with a specific key.

 bool Contains(System::String ^ Key);
public bool Contains (string Key);
member this.Contains : string -> bool
Public Function Contains (Key As String) As Boolean



必要。Required. String 運算式,其指定要搜尋集合項目的索引鍵。A String expression that specifies the key for which to search the elements of the collection.


如果 Visual Basic Collection 物件包含具有特定機碼的元素,則為 true;否則為 falsetrue if Visual Basic Collection object contains an element with a specific key; otherwise, false.


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.")
    MsgBox("The desired customer is not in the collection.")
End If

如果您想要使用索引鍵來搜尋集合中的元素,請記得在每次呼叫 Add 方法時提供 Key 引數。If you intend to search the collection for elements using their keys, remember to supply the Key argument every time you call the Add method.


如果集合包含索引鍵完全符合 Key的元素,Contains 會傳回 TrueContains returns True if the collection contains an element with a key exactly matching Key. 否則,Contains 會傳回 FalseOtherwise, Contains returns False. 符合索引鍵值時,會忽略大小寫。Case is ignored when matching key values.

Visual Basic Collection 可以保存某些具有索引鍵和其他元素的元素,但不含索引鍵。A Visual Basic Collection can hold some elements that have keys and other elements without keys. 這取決於呼叫 Add 方法是否提供選擇性 Key 參數的引數。This depends on whether the call to the Add method supplies an argument to the optional Key parameter.