HomeSearch

VB.NET Hashtable Type

Use Hashtable and its important methods. Hashtable provides fast lookups of keys.

Hashtable performs fast lookups from any key. It returns a previously specified value. Conceptually we add keys and values together. We then search the table instantly or remove elements. The Hashtable is an optimized lookup mechanism.

Example. To start, this example shows how to 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.Integer

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

Contains. 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. In the example all return true.

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

Get values. Typically, performing lookups on a Hashtable is the most common operation. This is efficient and well-optimized. In this example, we create a new hashtable. We add two key-value pairs to it.

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.

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.
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

TypeOf. How can you test what type a value you get from a Hashtable is? We use the TypeOf operator and the Is-operator. After successfully determining the type, you can then use another local variable to cast it and later use it.
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

Clear, Count. Often, 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.

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

TryCast. Consider this program. It adds two values of different types to a Hashtable. And it then uses the TryCast operator to cast the values. So this Hashtable can contain Strings and other Hashtables, nested ones.

Nothing: If TryCast succeeds, it returns the cast variable. If it fails, it returns Nothing: we can test for this value.

TryCast
VB.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

Benchmark. 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.

Tip: For specific benchmarking material, please visit the C# version of this article.

Hashtable

Summary. 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, the Hashtable is still in wide use.Dictionary
Home
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.