Common ASP.NET Code Techniques (DPC&DWC Reference)--6
n types can be iterated via the IEnumerator enumerator as well.
Because an enumerator's most basic purpose is to serve as a cursor for a collection, the IEnumerator class contains only a single property that returns the element in the collection to which the enumerator is currently pointing. (More specialized enumerators, such as IDictionaryElement, contain multiple properties.) IEnumerator contains just two methods: MoveNext, which advances the enumerator to the next element in the collection, and Reset, which returns the enumerator to its starting position—the position immediately before the first element in the collection.
Listing 2.1.8 contains a simple ASP.NET page that illustrates iteration through both an ArrayList and Hashtable with the IEnumerator enumerator. The output is shown in Figure 2.6.
Listing 2.1.8 To Step Through Each Element of a Collection, an Enumerator Can Be Used
1: <script language="VB" runat="server">
2:
3: Sub Page_Load(sender as Object, e as EventArgs)
4: ' Create some Collections
5: Dim aTeam1 as New ArrayList(), _
6: aTeam2 as New ArrayList(), _
7: aTeam3 as New ArrayList()
8:
9: Dim htProjects as New Hashtable()
10:
11: ' Assign memebers to the various teams
12: aTeam1.Add("Scott")
13: aTeam1.Add("Rob")
14: aTeam1.Add("Chris")
15:
16: aTeam2.Add("Doug")
17: aTeam2.Add("Don")
18:
19: aTeam3.Add("Billy")
20: aTeam3.Add("Mark")
21: aTeam3.Add("Charles")
22: aTeam3.Add("Steve")
23:
24:
25: ' Add each team to the htProjects HashTable
26: htProjects.Add("Prototyping", aTeam1)
27: htProjects.Add("Coding", aTeam2)
28: htProjects.Add("Testing", aTeam3)
29:
30: ' Now, list each project
31: Dim enumProjects as IEnumerator = htProjects.GetEnumerator()
32: Do While enumProjects.MoveNext()
33: lblProjectListing.Text &= enumProjects.Current.Key & "<br>"
34: Loop
35:
36: ' Now list each team
37: Dim enumTeam as IEnumerator
38: enumProjects.Reset()
39: Do While enumProjects.MoveNext()
40: lblDetailedListing.Text &= "<b>" & enumProjects.Current.Key
& ":</b><ul>"
41:
42: enumTeam = enumProjects.Current.Value.GetEnume
Because an enumerator's most basic purpose is to serve as a cursor for a collection, the IEnumerator class contains only a single property that returns the element in the collection to which the enumerator is currently pointing. (More specialized enumerators, such as IDictionaryElement, contain multiple properties.) IEnumerator contains just two methods: MoveNext, which advances the enumerator to the next element in the collection, and Reset, which returns the enumerator to its starting position—the position immediately before the first element in the collection.
Listing 2.1.8 contains a simple ASP.NET page that illustrates iteration through both an ArrayList and Hashtable with the IEnumerator enumerator. The output is shown in Figure 2.6.
Listing 2.1.8 To Step Through Each Element of a Collection, an Enumerator Can Be Used
1: <script language="VB" runat="server">
2:
3: Sub Page_Load(sender as Object, e as EventArgs)
4: ' Create some Collections
5: Dim aTeam1 as New ArrayList(), _
6: aTeam2 as New ArrayList(), _
7: aTeam3 as New ArrayList()
8:
9: Dim htProjects as New Hashtable()
10:
11: ' Assign memebers to the various teams
12: aTeam1.Add("Scott")
13: aTeam1.Add("Rob")
14: aTeam1.Add("Chris")
15:
16: aTeam2.Add("Doug")
17: aTeam2.Add("Don")
18:
19: aTeam3.Add("Billy")
20: aTeam3.Add("Mark")
21: aTeam3.Add("Charles")
22: aTeam3.Add("Steve")
23:
24:
25: ' Add each team to the htProjects HashTable
26: htProjects.Add("Prototyping", aTeam1)
27: htProjects.Add("Coding", aTeam2)
28: htProjects.Add("Testing", aTeam3)
29:
30: ' Now, list each project
31: Dim enumProjects as IEnumerator = htProjects.GetEnumerator()
32: Do While enumProjects.MoveNext()
33: lblProjectListing.Text &= enumProjects.Current.Key & "<br>"
34: Loop
35:
36: ' Now list each team
37: Dim enumTeam as IEnumerator
38: enumProjects.Reset()
39: Do While enumProjects.MoveNext()
40: lblDetailedListing.Text &= "<b>" & enumProjects.Current.Key
& ":</b><ul>"
41:
42: enumTeam = enumProjects.Current.Value.GetEnume
| 对此文章发表了评论 |

