Collection.Contains(String) Method

Definition

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

Parameters

Key
String

Required. A String expression that specifies the key for which to search the elements of the collection.

Returns

true if Visual Basic Collection object contains an element with a specific key; otherwise, false.

Examples

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

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.

Remarks

Contains returns True if the collection contains an element with a key exactly matching Key. Otherwise, Contains returns False. Case is ignored when matching key values.

A Visual Basic Collection can hold some elements that have keys and other elements without keys. This depends on whether the call to the Add method supplies an argument to the optional Key parameter.

Applies to