您现在的位置: 无忧电子商务网 >> 信息学院 >> 程序开发 >> asp.net >> 正文

Common ASP.NET Code Techniques (DPC&DWC Reference)--6

作者:作者:未…    信息学院来源:网络收集    点击数:    更新时间:2006-8-28 我要参与讨论

  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

上一页  [1] [2] [3] [4] [5] [6] 下一页

在google里搜索更多Common ASP.NET Code Techniques (DPC&DWC Reference)--6

Google
Web www.51ec.org
【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
我来说两句 对此文章发表了评论
  昵 称: *必填    ·注册用户·
  评 分: 1分 2分 3分 4分 5分     严禁发表危害国家安全、政治、黄色淫秽等内容的评论,用户需对自己在使用本网站服务过程中的行为承担法律责任。本站管理员有权保留或删除评论内容,评论内容只代表机友个人观点,与本网站立场无关。  
评 论
内 容

 
评论列表 (最新 评论仅限网友观点!)

供求信息




| 设为首页 | 加入收藏 | 关于我们 | 广告服务 | 联系方式 | 友情链接 | 版权申明