stu nicholls dot com | menu - Professional dropdown #3

click here


Hashtable

A Hashtable collection stores a (Key, Value) pair and uses the Key to hash and obtain the storage location. Each item in the Hashtable has a key and value pair. Hashtable performs fast lookups from any key. We add keys and values together. The Key is immutable and cannot have duplicate entries in the Hashtable. A Hashtable is used when you need to access elements by using key, and you can identify a useful key value. The key is used to access the items in the collection.

Following is the code for adding elements to the Hash Table: Fig 76 displays the result after the creation of the Hash Table when you execute the application.

Private Sub Hash_Table_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Hash_Table.Click
HT.Add("Nokia", 5000)
HT.Add("Samsung", 6000)
HT.Add("Huwei", 4000)
HT.Add("LG", 3000)
HT.Add("Lennova", 7000)
MsgBox("Hash Table Created")
End Sub

Fig83



Elements to the Hash Table can be added with the help of “With” keyword also which is given in the following code.

Private Sub Hash_Table_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Hash_Table.Click
With HT
.Add("Nokia", 5000)
.Add("Samsung", 6000)
.Add("Huwei", 4000)
.Add("LG", 3000)
.Add("Lennova", 7000)
End With
MsgBox("Hash Table Created")
End Sub

Following is the code for displaying the elements of the Hash Table which is shown in Fig. 84.

Private Sub btndisHT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndisHT.Click
Dim i
Dim s As String = ""
MsgBox("Displaying Hash Table")
For Each i In HT.Keys
s = s & i & " = " & HT(i) & ControlChars.CrLf
Next
MsgBox("Elements of Hash Table: " & ControlChars.CrLf & s)
End Sub
Fig84



Following is the code for removing the added elements from the Hash Table:
When the program is executed it asks through the Input Box for the key which you want to delete, which is shown in Fig.85. Then it will display key and its value which deleted from the Hash Table which is shown in Fig. 86.

Private Sub btnRemHT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemHT.Click
Dim str As String = "Enter the Key you want to Remove"
Dim i
For Each i In HT.Keys
str &= ControlChars.CrLf & i
Next
Dim j = InputBox(str)
MsgBox("Removing Element is : " & j & " = " & HT(j))
HT.Remove(j)
End Sub
Fig85



Fig86



Following is the complete program for creating, displaying and removing the elements from the Hash Table:

Public Class Status_from
Dim HT As New Hashtable
Private Sub Hash_Table_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Hash_Table.Click
HT.Add("Nokia", 5000)
HT.Add("Samsung", 6000)
HT.Add("Huwei", 4000)
HT.Add("LG", 3000)
HT.Add("Lennova", 7000)
MsgBox("Hash Table Created")
End Sub

Private Sub btndisHT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndisHT.Click
Dim i
MsgBox("Displaying Hash Table")
For Each i In HT.Keys
MsgBox(i & " = " & HT(i))
Next
End Sub

Private Sub btnRemHT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemHT.Click
Dim str As String = "Enter the Item you want to Remove"
Dim i
For Each i In HT.Keys
str &= ControlChars.CrLf & i
Next
Dim j = InputBox(str)
MsgBox("Removing Element is : " & j & " = " & HT(j))
HT.Remove(j)
End Sub
End Class

<< Previous


comments powered by Disqus
Footer1