Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Collection.Contains Method

Returns a Boolean value indicating whether a Visual Basic Collection object contains an element with a specific key.

Namespace:  Microsoft.VisualBasic
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

public bool Contains(
	string Key
)

Parameters

Key
Type: System.String

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

Return Value

Type: System.Boolean
Returns a Boolean value indicating whether a Visual Basic Collection object contains an element with a specific key.

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.

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.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft