Collection.Contains(String) Collection.Contains(String) Collection.Contains(String) Collection.Contains(String) Method

定义

返回一个值,该值指示 Visual Basic Collection 对象是否包含一个带有特定键的元素。Returns a value that indicates whether a Visual Basic Collection object contains an element with a specific key.

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

参数

Key
String String String String

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

如果要使用其键搜索元素的集合,请记住在每次Key Add调用方法时提供自变量。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.

注解

Contains如果True集合包含具有完全匹配Key的键的元素,则返回。Contains 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.

适用于