How to Use the Hashtable Class in ASP.NET

Tuesday, January 27, 2009

The Hashtable class is perhaps one of the most useful data structures inthe .NET Framework, even if the name is quite unusual. (If you’ve worked inother languages, you might know a type called map. The Hashtable in .NETis the same as a map in other languages.)A Hashtable instance lets you associate data. For example, if you want tostore a list of names and associate a salary with each name, you can use aHashtable. Or, if you want to store a list of vehicle ID numbers — and associateeach with the color of the car — you can use a Hashtable.
Following is some VB code that demonstrates putting a list of car IDs into aHashtable and associating a color with each:



Dim cars As New Hashtable

cars(“6789Q345254”) = “green”

cars(“35T893W2A21”) = “red”

cars(“T9U3W8OQQ53”) = “blue”

cars(“T5WQY374833”) = “blue”

Response.Write(“The car 6789Q345254 is “)

Response.Write(cars(“6789Q345254”))


The first line of this code creates a new Hashtable instance. The next fourlines put some data into the Hashtable instance. You can see the generalformat for inserting the data: You type the name of the Hashtable, followedby a key surrounded by parentheses. In this example, the keys are strings(and are supposed to be vehicle ID numbers, even though I made themshorter than real vehicle ID numbers) — but you can use any type you wantfor the keys.Next comes the equal sign, followed by the value you want to associate withthe key. Again, in this case, I’m using strings, but you’re free to use any type.You can see then that I’m associating the value “green” with the ID
“6789Q345254” using this line:

cars(“6789Q345254”) = “green”


Do you notice anything familiar with the Hashtable? The Hashtable worksvery much like an array — except instead of having to use only integers forthe index, you can use any type you want. (Oh, yes — instead of the termindex, which is associated with a Hashtable, the term is key.)To access the values in the Hashtable, you again put your key inside parentheses,just like you would with an array. Here’s an example line from thepreceding code:


Response.Write(cars(“6789Q345254”))


This code writes out the value for the key “6789Q345254” (which you cansee is “green”).Each element in a Hashtable instance has a unique key. No two elements canshare the same key. However, two elements can share the same value. Think ofthe car example: Two cars won’t share the same vehicle ID number, but twocars can have the same color. Knowing that keys are unique, you can probablyrealize how to change the value in a key: Just reassign it. Suppose you havethe following code (notice that the keys are the same in these two lines):


cars(“T5WQY374833”) = “blue”

cars(“T5WQY374833”) = “red”


Using the Hashtable ClassThe first line associates the string “blue” with a given car. Then the nextline associates the string “red” with the very same car. So which one winsout? The most recent one. The second line simply replaced the old color(blue) with the new color (red) — that is, by changing the value to thestring “red”.One way to understand the Hashtable class — and to recognize when youmight need it — is to think of the class as just an array whose indexes aretypes that don’t have to be integers.The VB codedemonstrates that you can use any type, — notjust integers — for the keys. The code then shows how to iterate throughall the elements in the Hashtable instance. (You can put this code in aPage_Load event to try it out.)
We will See more on Hashtable...

Read more...

Back to TOP