C# Hashtables
What Is It:
A Hashtable, unlike an array, will allow you to store data based on keys and values. Each key will have a value. You can use this to store Names, Numbers, anything in your life that would require organization and order! A HashTable is known as an associative array.
Getting Started:
The very first thing we want to do when working with a Hashtable..is to set it up!
Code
Hashtable hf = new Hashtable();By reading this code you should have a fairly basic understanding of what is does. It creates a new Hashtable object that is named "hf"
The next thing we want to do is start setting up our keys/values.
Code
hf["Predictable"] = "is cool";
hf["Hackforums"] = "is cooler";hf is obviously the name of our Hashtable. "Predictable" is one of the keys in our Hashtable, it is obviously named Predictable!
After the equals sign is our value, the value is the stuff that our key is holding.
Now, we want to display our key/value pairs in a MessageBox, Right?
NO! MessageBox's are getting boring. So, create a new listbox. A listbox is just a component that will display a list of something. To display our keys and values in our new listbox, we do the following:
Code
foreach (DictionaryEntry h in hf)
{
listBox1.Items.Add(h.Key + " " + h.Value);
}This is called a foreach loop. We use the word "DictionaryEntry" is what we use when using a foreach loop with a Hashtable, "h" is just the name of our new variable.
We use "listbox1.Items.Add" to add something to the listbox. "h.Key" and "h.Value" simply adds the key/value pairs to our listbox!
Conclusion:
Hashtables can be used for anything, whether it is storing people in your contacts, matching MD5 hashes with plaintext, or simply screwing around.
[IMG]http://i52.[censored].com/302xdl1.jpg[/IMG]
Code:
Code
private void button1_Click(object sender, EventArgs e)
{
Hashtable hf = new Hashtable();
hf["Predictable"] = "is cool";
hf["Hackforums"] = "is cooler";
foreach (DictionaryEntry h in hf)
{
listBox1.Items.Add(h.Key + " " + h.Value);
}
}http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.

