Hashtable: Contains |
Determines whether the Hashtable contains a specific key.
Public Function Contains( ByRef Key As Variant ) As Boolean
This method behaves exactly as ContainsKey.
This method is an O(1) operation.
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.