How to VB.Net HashTable

HashTable stores a Key Value pair type collection of data . We can retrive items from hashTable to provide the key . Both key and value are Objects.

The common functions using in Hashtable are :

Add : To add a pair of value in HashTable

  1. Syntax : HashTable.Add(Key,Value).
  2. Key : The Key value.
  3. Value : The value of corrosponding key.

ContainsKey : Check if a specified key exist or not

ContainsValue : Check the specified Value exist in HashTable

Remove : Remove the specified Key and corrosponding Value

The following source code shows all important operations in a HashTable

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
        'Search an Item
        If weeks.ContainsValue("Tue") Then
            MsgBox("Not find")
        End If
        'remove an Item
        'Display all key value pairs
        For Each day In weeks
            MsgBox(day.Key  "  --  "  day.Value)
    End Sub
End Class

When you execute this program it add seven weekdays in the hashtable and display the item 5. Then it check the item "Tue" is existing or not . Next it remove the third item from HashTable. Finaly it displays all item exist in the HashTable. (C) 2021    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About