Common asp.net Code techniques (DPC&DWC Reference)--3

Source: Internet
Author: User
Tags hash integer reference
asp.net 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 a 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 This Add method, which adds a element to the Hashtable collection, takes Two-parameters:the-a Alphanu Meric key by which the element would be referenced, and the second are the element itself, which needs to be of type Object.

In Listing 2.1.2, we are 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-example later in this chapter, we can even create collections of collections (collections whose element s 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 and the alphanumeric key of the element to is removed. Line demonstrates this behavior and removing the element referred to as "BILLG" in the hash table. On line We remove the elements of the hash table via the clear method. (Recall that all collection types contain a clear method of that demonstrates identical functionality.)

The Hashtable class contains two handy methods for determining a key or value whether. The 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 is used on line 24. The Hashtable class also supports a method called Containsvalue. This is accepts a single parameter of type Object and searches the hash table to-to-do-if any element contains that part Icular value. If it finds such an element, Containsvalue'll return True; Otherwise, it would return False. The ContainsKey and Containsvalue methods are used primarily for quickly determining whether a particular key or element E Xists in a Hashtable.

On line, a check is made to the if the key "Billg" existed before the Remove method is used. Checking to make sure a item exists before removing it is not required. If you use the Remove method to try to remove a element that is does not exist (for example, if we had Remove ("Homer") in Li Sting 2.2.1), no error or exception'll occur.

The Keys and Values collections
The Hashtable class exposes two collections as Properties:keys and Values. The Keys collection is, as it name suggests, a collection of all alphanumeric key values in a Hashtable. Likewise, the values collection is a collection the "all" element Values in a Hashtable. These two properties can is useful if you are are only interested in, say, listing the various keys.

On line at Listing 2.1.2, the DataSource property of the Dgemployees DataGrid was set to the Keys collection of the Hysa Laries Hashtable instance. Because the Keys property of the Hashtable class returns a ICollection interface, it can is bound to a DataGrid using dat A 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 to the. NET Framework:the class and the Hashtable class. Each of the collections indexes elements in a different manner. The ArrayList indexes each element numerically, whereas the Hashtable indexes all element with a alphanumeric key. The ArrayList orders each element sequentially, based to its numerical index; The Hashtable applies a seemingly random ordering (because the order was determined by a hashing algorithm).

What If you need a collection, though, which allows access to elements by both a 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 this insert elements in a particular SPO T. Rather, similar to the Hashtable class, there are only a single and add elements to the Collection:add. However, because the SortedList can is indexed by both key and value, the class contains both Remove and RemoveAt. As with all the other collection types, the SortedList also contains a clear method this removes all elements.

Because a sortedlist encapsulates the functionality of both the Hashtable and ArrayList, it's no classes that the C Lass provides a number of methods to access its elements. As with a Hashtable, the 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 a integral index, like with the ArrayList class. To get the value in a particular index, 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 is 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 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:
: ' Display a list of test scores
14:lblscores.text = "<i>there are" & Sltestscores.count & _
: "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:
A: ' has Edward taken the test? If So, reduce he grade by points
22:if Sltestscores.containskey ("Edward") Then
23:sltestscores ("edward") = Sltestscores ("Edward")-10
24:end If
25:
Num: ' Assume Sally cheated and remove her score from the list
27:sltestscores.remove ("Sally")
28:
: ' 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:
Grades: ' Display the new
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
: </script>
45:
: <body>
<b>raw Test results:</b><br>
<asp:label id= "Lblscores" runat= "Server"/>
: <p>
51:
<b>curved Test results:</b><br>
<asp:label id= "Lblcurvedscores" runat= "Server"/>
Si: </body>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.