Hashtable.Contains Method Determines whether the Hashtable contains a specific key.
Imports System
Imports System.Collections
Public Class SamplesHashtable
Public Shared Sub Main()
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")
PrintIndexAndKeysAndValues(myHT)
Dim myKey As Integer = 2
Console.Write("The key ""{0}"" is ", myKey)
If(myHT.ContainsKey(myKey))
Console.WriteLine("in the Hashtable.")
Else
Console.WriteLine("NOT in the Hashtable.")
End If
Dim myValue As String = "three"
Console.Write("The value ""{0}"" is ", myValue)
If(myHT.ContainsValue(myValue))
Console.WriteLine(" in the Hashtable.")
Else
Console.WriteLine(" NOT in the Hashtable.")
End If
End Sub
Public Shared Sub PrintIndexAndKeysAndValues(myHT As Hashtable)
Dim i As Integer = 0
Dim de As DictionaryEntry
For Each de In myHT
Console.WriteLine("{1}:{2}", i, de.Key, de.Value)
i = i + 1
Next de
Console.WriteLine()
End Sub
End Class
Related examples in the same category