SortedList: Contains

Contains

Determines whether a SortedList object contains a specific key.



 Public Function Contains(
	  ByRef Key As Variant ) As Boolean

Parameters

Key
[ByRef] Variant. The key to locate in the SortedList object.

Return Values

Boolean -  True if the SortedList object contains an element with specified Key; otherwise False.

Remarks

The elements of a SortedList object are sorted by the keys either according to a specific IComparer implementation specified when the SortedList is created.

This method behaves exactly as the ContainsKey method.

This method uses a binary search algorithm; therefore, this method is an O(log n) operation, where n is Count.

Exceptions

Exception Condition
InvalidOperationExceptionThe comparer throws an exception.

Examples

The following code example shows how to determine whether a SortedList object contains a specific element.

Public Sub Main()
    Dim List As New SortedList
    Dim TempArray() As String
    Dim TargetArray() As DictionaryEntry
    Dim i As Long
    
    List.Add 2, "cats"
    List.Add 3, "in"
    List.Add 1, "napping"
    List.Add 4, "the"
    List.Add 0, "three"
    List.Add 5, "barn"
    
    TempArray = NewStrings("The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog")
    ReDim TargetArray(0 To 14)
    
    For i = 0 To UBound(TargetArray)
        If i <= UBound(TempArray) Then
            Set TargetArray(i) = NewDictionaryEntry(i, TempArray(i))
        Else
            Set TargetArray(i) = NewDictionaryEntry(i, "")
        End If
    Next
    
    Debug.Print "The target Array contains the following (before and after copying):"
    PrintValues TargetArray
    
    List.CopyTo TargetArray, 6
    
    PrintValues TargetArray
End Sub

Private Sub PrintValues(ByRef Arr() As DictionaryEntry)
    Dim i As Long
    
    For i = LBound(Arr) To UBound(Arr)
        Debug.Print " "; Arr(i).Value;
    Next
    
    Debug.Print
End Sub

' The code produces the following output.
'
'    The target Array contains the following (before and after copying):
'     The quick brown fox jumped over the lazy dog
'     The quick brown fox jumped over three napping cats in the barn

See Also

Project CorLib Overview

Class SortedList Overview

ContainsKey

ContainsValue