- 12345
ASP.NET: Looping using an Enumerator
From Wiki
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:
- Partial Class Default1
- Inherits System.Web.UI.Page
- Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- ' Example array list
- Dim MyList As New ArrayList
- MyList.Add(1)
- MyList.Add(2)
- MyList.Add(3)
- MyList.Add(4)
- MyList.Add(5)
- ' Create an iEnumerator object
- Dim iEnum As IEnumerator = MyList.GetEnumerator
- ' Loop through the data using the iEnumerator
- While iEnum.MoveNext
- Response.Write(CInt(iEnum.Current))
- End While
- End Sub
- End Class
Output:
This Hack is part of the ASP.NET Hacks collection


