C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Finally: We use the DictionaryEntry type on the iteration variable and then access the Key and Value properties in the loop body.
VB.NET program that loops over Hashtable
Module Module1
Sub Main()
' Create an example Hashtable instance.
Dim table As Hashtable = New Hashtable
table(1) = "One"
table(2) = "Two"
table(15) = "Fifteen"
' Use For Each loop over the Hashtable.
For Each element As DictionaryEntry In table
Console.WriteLine(element.Key)
Console.WriteLine(element.Value)
Next
End Sub
End Module
Output
15
Fifteen
2
Two
1
One
Finally: We look up the value associated with the key "Area", which is the Integer 1000.
VB.NET program that uses Contains functions
Module Module1
Function GetHashtable() As Hashtable
Dim table As Hashtable = New Hashtable
table.Add("Area", 1000)
table.Add("Perimeter", 55)
table.Add("Mortgage", 540)
Return table
End Function
Sub Main()
' Create an example Hashtable instance.
Dim table As Hashtable = GetHashtable()
' Test Contains* functions.
Console.WriteLine(table.ContainsKey("Perimeter"))
Console.WriteLine(table.Contains("Area"))
Console.WriteLine(table.ContainsValue(55))
' Look up area.
Dim area As Integer = table("Area")
Console.WriteLine(area)
End Sub
End Module
Output
True
True
True
1000
Note: One key is of type Integer, and one key is of type String. One value is of type Integer, and one value is of type String as well.
VB.NET program that gets values from Hashtable
Module Module1
Sub Main()
Dim table As Hashtable = New Hashtable
table.Add(300, "Carrot")
table.Add("Area", 1000)
' Get string and integer values.
Dim value1 As String = table(300)
Dim value2 As Integer = table("Area")
' Display values.
Console.WriteLine(value1)
Console.WriteLine(value2)
End Sub
End Module
Output
Carrot
1000
VB.NET program that uses TypeOf, Hashtable
Module Module1
Sub Main()
Dim table As Hashtable = New Hashtable
table.Add(100, "Perl")
' Get string and integer values.
Dim value1 = table(100)
' Use TypeOf operator to test type of hashtable value.
If (TypeOf value1 Is String) Then
' Cast.
Dim value As String = value1
Console.WriteLine(value)
End If
End Sub
End Module
Output
Perl
Next: In this example, the Hashtable first contains three key-value pairs. We then clear it, and it contains zero pairs.
VB.NET program that uses Clear and Count
Module Module1
Sub Main()
Dim table As Hashtable = New Hashtable
table.Add(100, "Perl")
table.Add(200, "Dot")
table.Add(300, "Net")
' Current count.
Console.WriteLine(table.Count)
' Clear.
table.Clear()
' Current count is now zero.
Console.WriteLine(table.Count)
End Sub
End Module
Output
3
0
Nothing: If TryCast succeeds, it returns the cast variable. If it fails, it returns Nothing: we can test for this value.
TryCastVB.NET program that uses TryCast
Module Module1
Sub Main()
' Store reference types as values.
Dim table As Hashtable = New Hashtable
table(1) = "One"
table(2) = New Hashtable
' Cast Hashtable values to Hashtables.
Dim value As Hashtable = TryCast(table(1), Hashtable)
Dim value2 As Hashtable = TryCast(table(2), Hashtable)
' Value is Nothing.
If value Is Nothing Then
Console.WriteLine(1)
End If
' Value2 is not Nothing: the cast succeeded.
If Not value2 Is Nothing Then
Console.WriteLine(2)
End If
End Sub
End Module
Output
1
2
Tip: For specific benchmarking material, please visit the C# version of this article.
Hashtable