figure 2.2
output of listing 2.1.2 when viewed through a browser.
adding elements to a hashtable
in listing 2.1.2, we begin by creating an instance of the hashtable class, htsalaries, on line 5. next, we populate this hash table with our various employees and their respective salaries on lines 7 through 12. note that the add method, which adds an element to the hashtable collection, takes two parameters: the first is an alphanumeric key by which the element will be referenced, and the second is the element itself, which needs to be of type object.
in listing 2.1.2, we are storing integer values in our hashtable class. of course we are not limited to storing just simple data types; rather, we can store any type of object. as we'll see in an example later in this chapter, we can even create collections of collections (collections whose elements are also collections)!
removing elements from a hashtable
the hashtable class contains two methods to remove elements: remove and clear. remove expects a single parameter, the alphanumeric key of the element to be removed. line 25 demonstrates this behavior, removing the element referred to as "billg" in the hash table. on line 34 we remove all the elements of the hash table via the clear method. (recall that all collection types contain a clear method that demonstrates identical functionality.)
the hashtable class contains two handy methods for determining whether a key or value exists. the first function, containskey, takes a single parameter, the alphanumeric key to search for. if the key is found within the hash table, containskey returns true. if the key is not found, containskey returns false. in listing 2.1.2, this method is used on line 24. the hashtable class also supports a method called containsvalue. this method accepts a single parameter of type object and searches the hash table to see if any element contains that particular value. if it finds such an element, containsvalue will return true; otherwise, it will return false. the containskey and containsvalue methods are used primarily for quickly determining whether a particular key or element exists in a hashtable.
on line 24, a check was made to see if the key "billg" existed before the remove method was used. checking to make sure an item exists before removing it is not required. if you use the remove method to try to remove an element that does not exist (for example, if we had remove("homer") in listing 2.2.1), no error or exception will occur.
the keys and values collections
the hashtable class exposes two collections as properties: keys and values. the keys collection is, as its name suggests, a collection of all the alphanumeric key values in a hashtable. likewise, the values collection is a collection of all the element values in a hashtable. these two properties can be useful if you are only interested in, say, listing the various keys.
on line 30 in listing 2.1.2, the datasource property of the dgemployees datagrid is set to the keys collection of the hysalaries hashtable instance. because the keys property of the hashtable class returns an icollection interface, it can be bound to a datagrid using data binding. for more information on data binding and using the datagrid, refer to chapter 7, "data presentation."
working with the sortedlist class
so far we've examined two collections provided by the .net framework: the hashtable class and the arraylist class. each of these collections indexes elements in a different manner. the arraylist indexes each element numerically, whereas the hashtable indexes each element with an alphanumeric key. the arraylist orders each element sequentially, based on its numerical index; the hashtable applies a seemingly random ordering (because the order is determined by a hashing algorithm).
what if you need a collection, though, that allows access to elements by both an alphanumeric key and a numerical index? the .net framework includes a class that permits both types of access, the sortedlist class. this class internally maintains two arrays: a sorted array of the keys and an array of the values.
adding, removing, and indexing elements in a sortedlist
because the sortedlist orders its elements based on the key, there are no methods that insert elements in a particular spot. rather, similar to the hashtable class, there is only a single method to add elements to the collection: add. however, because the sortedlist can be indexed by both key and value, the class contains both remove and removeat methods. as with all the other collection types, the sortedlist also contains a clear method that removes all elements.
because a sortedlist encapsulates the functionality of both the hashtable and arraylist classes, it's no wonder that the class provides a number of methods to access its elements. as with a hashtable, sortedlist elements can be accessed via their keys. a sortedlist that stored integer values could have an element accessed similar to the following:
dim sortedlistvalue as integer
sortedlistvalue = slsortedlistinstance(key)
the sortedlist also can access elements through an integral index, like with the arraylist class. to get the value at a particular index, you can use the getbyindex method as follows:
dim sortedlistvalue as integer
sortedlistvalue = slsortedlistinstance.getbyindex(iposition)
iposition represents the zero-based ordinal index for the element to retrieve from slsortedlistinstance. additionally, elements can be accessed by index using the getvaluelist method to return a collection of values, which can then be accessed by index:
dim sortedlistvalue as integer
sortedlistvluae = slsortedlistinstance.getvaluelist(iposition)
listing 2.1.3 illustrates a number of ways to retrieve both the keys and values for elements of a sortedlist. the output is shown in figure 2.3.
listing 2.1.3 a sortedlist combines the functionality of a hashtable and arraylist
1: <script language="vb" runat="server">
2: sub page_load(sender as object, e as eventargs)
3: ' create a sortedlist
4: dim sltestscores as new sortedlist()
5:
6: ' use the add method to add students' test scores
7: sltestscores.add("judy", 87.8)
8: sltestscores.add("john", 79.3)
9: sltestscores.add("sally", 94.0)
10: sltestscores.add("scott", 91.5)
11: sltestscores.add("edward", 76.3)
12:
13: ' display a list of test scores
14: lblscores.text = "<i>there are " & sltestscores.count & _
15: " students...</i><br>"
16: dim dictentry as dictionaryentry
17: for each dictentry in sltestscores
18: lblscores.text &= dictentry.key & " - " & dictentry.value & "<br>"
19: next
20:
21: 'has edward taken the test? if so, reduce his grade by 10 points
22: if sltestscores.containskey("edward") then
23: sltestscores("edward") = sltestscores("edward") - 10
24: end if
25:
26: 'assume sally cheated and remove her score from the list
27: sltestscores.remove("sally")
28:
29: 'grade on the curve - up everyone's score by 5 percent
30: dim iloop as integer
31: for iloop = 0 to sltestscores.count - 1
32: sltestscores.getvaluelist(iloop) = _
33: sltestscores.getvaluelist(iloop) * 1.05
34: next
35:
36: 'display the new grades
37: for iloop = 0 to sltestscores.count - 1
38: lblcurvedscores.text &= sltestscores.getkeylist(iloop) & " - " & _
39: string.format("{0:#.#}", sltestscores.getbyindex(iloop))
& "<br>"
40: next
41:
42: sltestscores.clear() ' remove all entries in the sorted list...
43: end sub
44: </script>
45:
46: <html>
47: <body>
48: <b>raw test results:</b><br>
49: <asp:label id="lblscores" runat="server" />
50: <p>
51:
52: <b>curved test results:</b><br>
53: <asp:label id="lblcurvedscores" runat="server" />
54: </body>
55: </html>