How to Use a HashTable in a Visual Basic Application


A hash table is a data structure that uses an associate array to map unique "key-value" pairs and store data. For example, an athlete who finishes first (the key) wins a gold medal (the value). Because there can only be one first-place winner, you guarantee that you will not duplicate that specific key in the hash table. Hash tables reduce the overhead of searching for data compared to some other data structures. Visual Basic has a HashTable class with several useful methods that you can implement in your projects instead of creating your own from scratch.

  • Open a new Visual Basic project. Double-click the "Button" control four times to add "Button1" through "Button4" to the form and arrange them as you like. Change the "Text" property for "Button1" to "Add," for "Button2" to "Remove," for "Button3" to "Show All" and for "Button4" to "Clear."

  • Press "F7" to open the code window. Type the following code on the first line:

    Imports System.Collections

    This imports the System.Collections namespace into the project. It contains the Hashtable class.

  • Open the "Form1" class and type the following code:

    Dim MyHashTable As New Hashtable()

    This creates a new object called "MyHashTable" that uses the Hashtable class.

  • Open the "Button1_Click" subroutine and type the following code:


    MyHashTable.Add("First", "Gold")

    MyHashTable.Add("Second", "Silver")

    MyHashTable.Add("Third", "Bronze")

    Catch ex As Exception

    MessageBox.Show("Duplicate Key")

    End Try

    These lines use the "Add" method to add three new items to the hash table with "First," "Second" and "Third" as the keys and "Gold," "Silver" and "Bronze" as their respective values. You can get the key-value pairs from anywhere else in the program, such as a user InputBox, TextBox controls or other selections at run time. Adding these items in a "try-catch" block ensures that if you try to add an item with a key that already exists in the hash table, it does not crash the program.

  • Open the "Button2_Click" subroutine and type the following code:

    If (MyHashTable.Count = 0) Then

    MsgBox("HashTable is empty")



    End If

    These lines use the "Count" method to see if the hash table is empty already. If so, the program tells the user; if not, it removes the most recent item. You can program alternative ways to remove items from the hash table.

  • Open the "Button3_Click" subroutine and type the following code:

    If MyHashTable.Count = 0 Then

    MsgBox("No items in HashTable")


    For i = 0 To MyHashTable.Count - 1

    MsgBox(MyHashTable.Keys(i).ToString & ", " & MyHashTable.Values(i))

    Next i

    End If

    These lines check first to see if any entries exist in the hash table. If not, the user is informed that the table is empty; otherwise, all of the key-value pairs are displayed. Because hash tables use zero-indexing, you need to count up to this hash table's "count" minus one.

  • Open the "Button4_Click" subroutine and type the following code:


    This method clears all entries in the hash table. You do not need to check first if the table is empty or not because clearing an empty table does not cause an error.

Related Searches


Promoted By Zergnet


You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

Is DIY in your DNA? Become part of our maker community.
Submit Your Work!