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:
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:
Catch ex As Exception
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")
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))
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.
How to Run a Command on a Remote System in Visual Basic
Visual Basic is a programming language built into Windows that allows you to easily automate a wide variety of tasks. Skilled Windows...
How to Make a Chat Room in Visual Basic Express
Visual Basic Express is available for free to anyone interested in creating applications using VB.NET. ... ByVal cList As Hashtable) Me.clientSocket =...
Visual Basic Property Bag Tutorial
How to Use a HashTable in a Visual Basic Application; How to Make a VB6 OCX; What Is OCX in Visual Basic?...
How to Transfer Excel VB Data From Range to Array
An array is a data structure that enables you to work with sets of data values in a single structure ... When...
What Are Methods of Visual Basic?
How to Use a HashTable in a Visual Basic Application. A hash table is a data structure that uses an associate array...
How to Read a Tab Delimited File in Java
How to Read a Line of Tab-Delimited Text in VBA. How to Read a Tab Delimited File in Java; How to Create...