1. Home
  2. Computing & Technology
  3. Visual Basic
VB .NET Hashtable - Part Two Of A Series
The Hashtable Object Completes the Picture

What are called, ADT's or Abstract Data Types have been evolving for years starting with Collections in VB and Dictionary Objects in VBScript. VB .NET's Hashtable object is now the state of the art. Part One of this series discusses Arrays, the Collection object and the Dictionary object. This article concludes with a detailed look at the VB .NET Hashtable object.


To meet the requirements of storing both data and objects, better performance, and the ability to make more methods and properties available to you, the programmer, VB .NET offers the Hashtable object. One big advantage of the Hashtable object is shared by all of VB .NET: It's inheritable.

With arrays, collections, and dictionary objects in VB 6, it's pretty much "what you see is what you get." But with Hashtable, you get an object that is part of a whole family of objects in the .NET namespace System.Collections. One of the best references to the deepest internals of .NET can be found in the O'Reilly book, VB.NET Core Classes in a Nutshell. Here's the 'Nutshell' view of System.Collections. (The Hashtable class is emphasized.)

System.Collections

Covering all these is way beyond the scope of this tutorial. (But keep checking the page for future articles!) I'll demonstrate the familiar problem we used in part one; this time using Hashtable, and then show you some of the additional capabilities that Hashtable provides.

But first, the concept of "hashing" in general needs to be clearly understood. A hash (or 'hash value') is a number generated from a string of text. The string could have numbers in it, but for hashing, consider them to be just text characters. The hash value is calculated so that it's unlikely - but not impossible - for some other text string to result in the same hash value. But the calculation guarantees that the same string will always produce the same hash value. Think of the hash as being a nickname for the entire string - a nickname on steroids.

A hash value is usually seen in security programs where it's necessary to make sure that some string, such a transmitted message, hasn't been changed. The usual process is that the sender will create a hash of the message text string and then encrypt the hash. Both the encrypted hash and the string are sent to a recipient. The recipient then decrypts the hash, produces another hash using the same hashing algorithm from the received message, and compares the two hashes. If they're the same, the message is considered to be exactly the same as the one transmitted.

But hashing is used for more than security. It's also pretty handy for structured storage and ADT's. Let's look at our original problem again.

Array Problem

Given the codes for parts, (Th, Do, and Gi), find the expanded name (Thingy, Dohicky, and Gizmo).

We demonstrated several different code examples to do this in part one, but let's look a little more closely at what's happening behind the scenes. Note: in the examples below, I describe what the Hashtable object does in general terms, but one of the principals of Object Oriented Programming is that the internal function of an object is a "black box" and you can only access the interface (methods and properties). So it is with Hashtable. The internals I describe can't actually be used.

First, VB .NET creates an index value for these records. The index values are calculated using a hash value. As an example, it's possible to use the default VB.NET hashing algorithm for strings in .NET to obtain a hash value for each of the keys.

Hash Values

The Microsoft documentation is full of warnings about using the GetHashCode method without overriding it, so read the documentation carefully before you do that. The method is actually intended to be overridden with a custom algorithm designed specifically for your application. Cryptologists can argue endlessly about the relative merits of different hashing algorithms. Be aware that you're stepping on territory ruled by very specialized technology.

Once a hash code value has been calculated for a member of a Hashtable, it's placed into a 'bucket' based on the hash code of the key. When the member is retrieved, the hash code value makes it easy to find the member in just that one bucket. Remember that it's possible for more than one member to be stored in the same bucket since hash values are not guaranteed to be unique.

The normal way to use the Hashtable object can be illustrated with the our Part Code Array example. Here the code to do this using Hashtable.

Using Hashtable
Dim strPartCode As String
Dim strPartName As String
strPartCode = InputBox("Enter the Part Code:")
'strPartCode = "Do"
strPartName = PartName(strPartCode)
If strPartName = "" Then
    MsgBox("The Part Code: " _
        & strPartCode _
        & " could not be found")
Else
    MsgBox("Part Code: " _
        & strPartCode _
        & " -- Expanded Name: " _
        & strPartName)
End If

Both the code and the end result is almost identical to the VBScript Dictionary object, including a requirement to declare the object:

Dim PartName As New Hashtable

The primary benefit of Hashtable is simply that it's been fine tuned in .NET to make it faster and more flexible. Compare the number of methods and properties of the latest VB 6 level object, Dictionary with Hashtable and you'll see what I mean. Besides, Hashtable is thread-safe (meaning that you can scale applications up to multiserver and networked capability.

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2009 About.com, a part of The New York Times Company.

All rights reserved.