Hashtable Class*

You will try out some of the built in hash table methods.

Create a new Visual Basic Widows forms Application and place a button on the form.

In the button’s event handler, create a new instance of the built in HashTable class:

Dim ht As New Hashtable

Write code to Add several key value pairs into the hash table:

ht.Add("aardvark", "a long nosed ant eating mamal")
ht.Add("cat", "an animal that eats cat food and goes meow")
ht.Add("zebra", "a stripy animal that looks a bit like a horse")

NOTE:
The Add method of the hash table includes a hashing algorithm and a collision resolution policy. This all happens behind the scenes.

Write code to retrieve a value based on the key:

'select a value based on the key. This is the fast lookup
Dim st As String
st = ht.Item("cat")
MsgBox(st)

NOTE:
The Item method of the hash table includes a hashing algorithm and a collision resolution policy. This all happens behind the scenes.

Test your code.

Write code to iterate through the keys sequentially:

'display values sequentially
For Each k In ht.Keys
    MsgBox(k)
Next

Test your code.

Write code to iterate through the values sequentially:

'display values sequentially
For Each v In ht.Values
    MsgBox(v)
Next

Test your code.

You will now create a form based glossary of terms that gets its data from a text file.

Create a simple text file called words.txt with some terms and definitions inside it like this. Note that each word and the corresponding definition are separated by a comma.

Place a listbox and a multi-line text box on your form and name them lbTerms and txtDefinition respectively.

Create a new instance of the built in Hashtable class called htGlossary with form level scope.

Write a new procedure called BuildHashTable which takes a Hashtable object as a parameter and populates it with data from the text file. This procedure uses a StreamReader object to read the file, line by line. Each time a line is ready inside the loop, the line is split into two at the comma position and the two portions are loaded into an array of two elements. The contents of the array are then used to populate the Hashtable:

   Sub BuildHashTable(ByRef g As Hashtable)
        Dim DictionaryReader As StreamReader
        Dim stLine As String
        Dim astWord() As String
        DictionaryReader = File.OpenText("H:\words.txt")

        Do While Not DictionaryReader.EndOfStream
            stLine = DictionaryReader.ReadLine
            astWord = stLine.Split(",")
            htGlossary.Add(astWord(0), astWord(1))
        Loop
        DictionaryReader.Close()
    End Sub

Write a new procedure called PopulateListBox which copies the keys from the Hashtable into an array variable then uses this array to populate the list box on the form with these keys as follows:

Sub PopulateListBox(ByVal g As Hashtable)
        'copy the keys from hashtable to an array
        Dim words(99) As Object
        g.Keys.CopyTo(words, 0)

        'add the words to the listbox
        Dim i As Integer
        For i = 0 To words.GetUpperBound(0)
            If Not (words(i) Is Nothing) Then
                lbTerms.Items.Add(words(i))
            End If
        Next
    End Sub

In the form’s load event handler, call the BuildHashTable and the PopulateListBox procedures one after another.

Test your form.

In the SelectedIndexChanged event handler of the listbox, use the word selected by the user to update the definition textbox as follows:

   Private Sub lstWords_SelectedIndexChanged(ByVal sender As system.Object, ByVal e As System.EventArgs) Handles lbTerms.SelectedIndexChanged
        Dim word As Object
        word = Me.lbTerms.SelectedItem
        Me.txtDefinition.Text = CStr(htGlossary.Item(word))
    End Sub

Extension Task 1

Write a person class with FirstName, LastName, DateOfBirth and Gender properties. Create several instances of the person class, each with a different set of property values. Create a Hashtable to store these objects using the FirstName as the key. Test this to make sure you can add and retrieve people.

Extension Task 2

Do some experimentation to discover what other methods and properties of the Hashtable are available. Do some research to find out how the VB.NET Hashtable class is implemented behind the scenes. For example, what hash algorithm is used; how are collisions dealt with?