Visual Basic has had great support for collections - dictionaries, arrays, and hashtables - for many years now. The information about how to use some of these tools isn't so great, however. I've written several articles in the past to help coders gets started using VB collections: Arrays, the Collection object, the Dictionary object - VB.NET Hashtable. These articles are fairly basic and don't cover the details about how the Hashtable object actually works. And, after quite a bit of searching, I couldn't find any other page on the web that did either. So this page takes it to the next level and tells you how to get the most from Hashtable.
You might also be interested in this article as an example of the use of XML in VB.NET. I use XML literals to create a file that can be easily imported and graphed in Excel. Or you might be interested in how I designed the program using the Stopwatch component to time the operation. The program details are explained after the Hashtable object.
The latest version of VB.NET (VB.NET 2008 - Framework 3.5), lists 15 overloads for Hashtable, but it's not quite that complicated. There are basically five parameters that you can mix and match that are passed to the Hashtable constructors.
- Hashcodeprovider
- Comparer
- IDictionary
- Capacity
- Loadfactor
To understand what these parameters do, you need to understand the purpose of Hashtable first. You might want to read the second article linked above for more detail, but here's the really quick version.
Hashtables let you create a fast, keyed array where the elements in the array can all be located by VB.NET using a key value. When you access a VB collection using a key, a calculation - called a "hashing algorithm" - points right at the element. Some documentation refers to this as a O(1) lookup rather than an array's O(n) lookup. O(n) just means that the code has to search through an array from the beginning to find an element so it takes longer.
Hashcodeprovider lets you use your own hashing algorithm instead of the one VB.NET provides. The hashing algorithm used to decide where to store elements is a critical part of the processing. VB.NET provides algorithms for you automatically for each different type. You can't actually see what the algorithm is (that's buried in the .NET Framework code) but you can see the keys calculated and you can overrride .NET and use your own if you like. You will only want to do that in rare and specialized situations. Nearly all of the time, using .NET's algorithm will work just fine.
If you want to see the keys that VB.NET generates, there's a method for that too. In the code example I use later, these commands show how to display the keys for the first few elements in the Immediate box:
? keyarray(0).GetHashCode
1683612090
? keyarray(1).GetHashCode
1224920486
? keyarray(2).GetHashCode
-374902149
Comparer is similar in that it's only necessary in unusual situations. This lets you create a custom way of deciding when one element is "greater" than another. You might use this for a language that has a unique sort sequence or perhaps as different way to categorize elements instead of storing a category as part of the Hashtable record. That is, by using a custom Comparer, you could code something like ...
If myHashtable.record1 > myHashtable.record2 Then ...
... instead of ...
If myHashtable.record1.category = thisCategory And
myHashtable.record2.category = thatCatory Then ...
Use the IDictionary parameter to copy elements from the specified dictionary (a different kind of collection that you might have access to in your code) to a new Hashtable object. This can be combined with other parameters to customize the new Hashtable.
On the next page, we consider the two parameters that reall can affect performance, Capacity and Loadfactor.

