Condividi tramite


Collection.Contains Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

'Declaration
Public Function Contains ( _
    Key As String _
) As Boolean
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.

Remarks

Contains returns True if the collection contains an element with a key exactly matching Key. Otherwise, Contains returns False.

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.

Examples

Dim Msg As String
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
  Msg = "The desired customer is in the collection."
Else
  Msg = "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.

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.