
The Hashtable collection in VB.NET offers the ability to perform fast and deterministic lookups from any key to a previously specified value. Conceptually, you add keys and values together, and then can search the table instantly, or even remove elements. The Hashtable provides a powerful and optimized lookup mechanism.
This VB Hashtable tutorial demonstrates important methods. Hashtable provides fast lookups of keys.
To start, this example shows how you can allocate a new Hashtable upon the managed heap. It then adds three key-value pairs to the data structure; the integer 1 is associated with the string literal "One", for example. Finally, the Hashtable is enumerated: we use the DictionaryEntry type on the iteration variable and then access the Key and Value properties in the loop body.
Program that loops over Hashtable [VB.NET]
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
OneHere, we test various functions on the Hashtable that determine the existence of certain keys: we use the ContainsKey, Contains, and ContainsValue functions. Each of these returns a Boolean value, and in the example all return true because the arguments were located. Finally, we look up the value associated with the key "Area", which is the Integer 1000.
Program that uses Contains functions [VB.NET]
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
1000Typically, performing lookups on a Hashtable is the most common operation, and this is well optimized. In this example, we create a new hashtable and add two key-value pairs to it; 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. Next, we employ the As syntax to assign a type to the values returned from the lookup. Then, we display the results to the screen. If the values returned did not have a matching type, an exception would occur.
Program that gets values from Hashtable [VB.NET]
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")
' Output.
Console.WriteLine(value1)
Console.WriteLine(value2)
End Sub
End Module
Output
Carrot
1000How can you test what type a value you get from a Hashtable is? One way you can do this in the VB.NET programming language is with the TypeOf operator and the Is operator. After successfully determining the type, you can then use another local variable to cast it and use it later on.
Program that uses TypeOf with Hashtable [VB.NET]
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
PerlOften, you will want to determine how many key-value pairs are contained in the Hashtable, or even erase all the pairs. To do these tasks, you can use the Count property, as well as the Clear function. In this example, the Hashtable first contains three key-value pairs; we then clear it, and it contains zero pairs.
Program that uses Clear and Count [VB.NET]
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
How well does the Hashtable collection perform in benchmarks? Unfortunately, the Hashtable is typically slower than the newer Dictionary type. This is because the casting overhead is always present; the keys and values are not of standardized types throughout the instance. For specific benchmarking material, please visit the C# version of this article.
Hashtable Examples
As a fast lookup data structure in the VB.NET language, the Hashtable provides essential functionality for certain search algorithms. Even though it has been largely replaced by the Dictionary type, which is faster, the Hashtable is still in wide use in important programs.
Dictionary Examples VB.NET Tutorials