Hashtable: ContainsValue

ContainsValue

Determines whether the Hashtable contains a specific value.



 Public Function ContainsValue(
	  ByRef Value As Variant ) As Boolean

Parameters

Value
[ByRef] Variant. The value to locate in the Hashtable.

Return Values

Boolean -  True if the Hashtable contains an element with the specified value; otherwise, False.

Remarks

The values of the elements of the Hashtable are compared to the specified value using the Object.Equals method.

This method performs a linear search; therefore, this method is an O(n) operation, where n is Count.

Examples

The following example shows how to determine whether the Hashtable contains a specific element.

Public Sub Main()
    ' Creates and initializes a new Hashtable.
    Dim MyHT As New Hashtable
    MyHT.Add 0, "zero"
    MyHT.Add 1, "one"
    MyHT.Add 2, "two"
    MyHT.Add 3, "three"
    MyHT.Add 4, "four"

    ' Displays the values of the Hashtable.
    Debug.Print "The Hashtable contains the following values:"
    PrintIndexAndKeysAndValues MyHT

    ' Searches for a specific key.
    Dim myKey As Integer
    myKey = 2
    Debug.Print CorString.Format("The key ""{0}"" is ", myKey);
    If MyHT.ContainsKey(myKey) Then
       Debug.Print "in the Hashtable."
    Else
       Debug.Print "NOT in the Hashtable."
    End If

    myKey = 6
    Debug.Print CorString.Format("The key ""{0}"" is ", myKey);
    If MyHT.ContainsKey(myKey) Then
       Debug.Print " in the Hashtable."
    Else
       Debug.Print " NOT in the Hashtable."
    End If

    ' Searches for a specific value.
    Dim MyValue As String
    MyValue = "three"
    
    Debug.Print CorString.Format("The value ""{0}"" is ", MyValue);
    If MyHT.ContainsValue(MyValue) Then
       Debug.Print " in the Hashtable."
    Else
       Debug.Print " NOT in the Hashtable."
    End If

    MyValue = "nine"
    Debug.Print CorString.Format("The value ""{0}"" is ", MyValue);
    If MyHT.ContainsValue(MyValue) Then
       Debug.Print " in the Hashtable."
    Else
       Debug.Print " NOT in the Hashtable."
    End If
End Sub

Public Sub PrintIndexAndKeysAndValues(ByVal MyHT As Hashtable)
    Dim de As DictionaryEntry
    Dim i As Long
    
    Debug.Print t("\t-INDEX-\t-KEY-\t-VALUE-")
    
    For Each de In MyHT
        Debug.Print CorString.Format(t("\t[{0}]:\t{1}\t{2}"), i, de.Key, de.Value)
        i = i + 1
    Next
    
    Debug.Print
End Sub


' This example code produces the following output.
'
'    The Hashtable contains the following values:
'        -INDEX- -KEY-   -VALUE-
'        [0]:    0   zero
'        [1]:    1   one
'        [2]:    2   two
'        [3]:    3   three
'        [4]:    4   four
'
'    The key "2" is in the Hashtable.
'    The key "6" is  NOT in the Hashtable.
'    The value "three" is  in the Hashtable.
'    The value "nine" is  NOT in the Hashtable.

See Also

Project CorLib Overview

Class Hashtable Overview

ContainsKey