Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

ASP.NET: Looping using an Enumerator

From Wiki

Jump to: navigation, search

Summary: Looping using an Enumerator

An iEnumerator object can be used on any object that implements iEnumerable. This can come in very handy as you will find many objects do implement this. As a simple example, here's a way you can add items to an ArrayList and then iterate through the item collection using an iEnumerator:

  1. Partial Class Default1
  2.     Inherits System.Web.UI.Page
  3.  
  4.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  5.  
  6.         ' Example array list
  7.         Dim MyList As New ArrayList
  8.         MyList.Add(1)
  9.         MyList.Add(2)
  10.         MyList.Add(3)
  11.         MyList.Add(4)
  12.         MyList.Add(5)
  13.  
  14.         ' Create an iEnumerator object
  15.         Dim iEnum As IEnumerator = MyList.GetEnumerator
  16.  
  17.         ' Loop through the data using the iEnumerator
  18.         While iEnum.MoveNext
  19.             Response.Write(CInt(iEnum.Current))
  20.         End While
  21.  
  22.     End Sub
  23.  
  24. End Class

Output:

  1. 12345


This Hack is part of the ASP.NET Hacks collection

489 Rating: 3.1/5 (7 votes cast)