You use an Hashtable when you want to store information based on Key/Value pairs. For example, the name of a student AND the score in an exam. This allows you to mix text and numbers. (In other programmes, Hashtables are known as associative arrays.)
Add a new button to your form, and double click it to get at the code. We'll add code for an Hashtable.You start by setting up the Hashtable:
Hashtable students = new Hashtable();
This creates a new object called students. It's going to be an Hashtable object.There are two ways you can add data to your Hashtable. Like this:
students["Jenny"] = 87;
students["Peter"] = "No Score";
students["Mary Jane"] = 64;
students["Azhar"] = 79;
Or like this:students["Peter"] = "No Score";
students["Mary Jane"] = 64;
students["Azhar"] = 79;
students.Add("Jenny", 87);
students.Add("Peter", "No Score";);
students.Add("Mary Jane", 64);
students.Add("Azhar", 79);
The first method uses a pair of square brackets:students.Add("Peter", "No Score";);
students.Add("Mary Jane", 64);
students.Add("Azhar", 79);
students["Jenny"] = 87;
In between the square brackets, you type what's known as the Key. So this particular entry in the Hashtable is called "Jenny". After an equals sign, you then type the Value that this Key will hold. Notice that three of the entries are number values, and one (Peter) is text.The second way to store values in an Hashtable is to use the Add Method:
students.Add("Jenny", 87);
In between the round brackets of Add( ), you first type the Key name. After a comma, you type the Value for that Key.There is a difference between the two. If you use the Add method, you can't have duplicate Key names. But you can if you use the square brackets. So this will get you an error:
students.Add("Jenny", 87);
students.Add("Jenny", 35);
But this won't:students.Add("Jenny", 35);
students["Jenny"] = 87;
students["Jenny"] = 35;
To try Hashtables out for yourself, add the following code to your button:students["Jenny"] = 35;
Hashtable students = new Hashtable();
students["Jenny"] = 87;
students["Peter"] = "No Score";
students["Mary Jane"] = 64;
students["Azhar"] = 79;
foreach (DictionaryEntry child in students)students["Peter"] = "No Score";
students["Mary Jane"] = 64;
students["Azhar"] = 79;
{
listBox1.Items.Add("student: " + child.Key + " , Score: " + child.Value);
}
Before running the code, have a look at the foreach loop. Inside of the round brackets, we have this:
DictionaryEntry child
This sets up a variable called child. But note the type of variable it is: a DictionaryEntry. C# uses an object of this type when using the foreach loop with Hashtables. That's because it automatically returns both the Key and the Value.Notice, too, what we have between the round brackets of the listbox's Add method:
"student: " + child.Key + " , Score: " + child.Value
The red bold are the important parts. After typing the name of your variable (child, for us) and a full stop, the IntelliSense list will appear. Key is a property that returns the name of your Key, and Value is a property that returns whatever you placed inside of that Key. Run your programme and click your button. The form on your listbox will then look like this:
Notice that the list of items has been sorted automatically. But the sort is done on the Keys, not on the Values.
But just like the ArrayList, you can Add new items, and remove old ones. To Remove an item, you do it like this:
students.Remove("Peter");
So you refer to the Key name, and not the Value, when you use the Remove method.
No comments:
Post a Comment