How to VB.Net HashTable

In VB.NET, a HashTable is a collection class that represents a collection of key-value pairs, where each unique key maps to a specific value. It provides an efficient way to store and retrieve data based on these key-value associations. The HashTable class is part of the System.Collections namespace in the .NET Framework.

The common functions using in Hashtable are :

Add

The Add function is used to add a key-value pair to the HashTable.

Dim myHashTable As New Hashtable() myHashTable.Add("John", 25)

Remove

The Remove function is used to remove a key-value pair from the HashTable based on the specified key.

myHashTable.Remove("John")

ContainsKey

The ContainsKey function checks if a specified key exists in the HashTable.

If myHashTable.ContainsKey("John") Then Console.WriteLine("John exists in the HashTable") End If

Item (Indexer)

The Item property (also known as the indexer) allows you to access the value associated with a specific key in the HashTable.

Dim age As Integer = myHashTable("John") Console.WriteLine("Age of John: " & age)

Count

The Count property returns the number of key-value pairs present in the HashTable.

Console.WriteLine("Number of key-value pairs: " & myHashTable.Count)

Clear

The Clear function removes all key-value pairs from the HashTable, making it empty.

myHashTable.Clear()

Keys and Values

The Keys and Values properties return collections that contain all the keys and values in the HashTable, respectively.

For Each key As Object In myHashTable.Keys Console.WriteLine("Key: " & key & ", Value: " & myHashTable(key)) Next

ContainsValue

The ContainsValue function checks if a specified value exists in the HashTable.

If myHashTable.ContainsValue(25) Then Console.WriteLine("The value 25 exists in the HashTable") End If

Clone

The Clone function creates a shallow copy of the HashTable.

Dim copyHashTable As Hashtable = DirectCast(myHashTable.Clone(), Hashtable)

TryGetValue

The TryGetValue function retrieves the value associated with a specific key from the HashTable, if it exists.

Dim age As Integer If myHashTable.TryGetValue("John", age) Then Console.WriteLine("Age of John: " & age) Else Console.WriteLine("John does not exist in the HashTable") End If

The following source code shows all important operations in a HashTable

Full Source VB.NET
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim weeks As New Hashtable Dim day As DictionaryEntry weeks.Add("1", "Sun") weeks.Add("2", "Mon") weeks.Add("3", "Tue") weeks.Add("4", "Wed") weeks.Add("5", "Thu") weeks.Add("6", "Fri") weeks.Add("7", "Sat") 'Display a single Item MsgBox(weeks.Item("5")) 'Search an Item If weeks.ContainsValue("Tue") Then MsgBox("Find") Else MsgBox("Not find") End If 'remove an Item weeks.Remove("3") 'Display all key value pairs For Each day In weeks MsgBox(day.Key " -- " day.Value) Next End Sub End Class

Conclusion

The HashTable in VB.NET provides a powerful data structure for efficient storage and retrieval of data based on key-value associations. It is suitable for various scenarios where fast lookup and dynamic sizing are required.