Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

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:


This Hack is part of the ASP.NET Hacks collection

489 Rating: 3.4/5 (8 votes cast)