O'Reilly logo

Visual Basic 2008 Recipes: A Problem-Solution Approach by Rakesh Rajan, Matthew MacDonald, Allen Jones, Todd Herman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

14.4. Implement an Enumerable Type Using a Custom Iterator

Problem

You need to create a collection type whose contents you can enumerate using a For Each statement.

Solution

Implement the interface System.Collections.IEnumerable or System.Collections.Generic.IEnumerable(Of T) on your collection type. The GetEnumerator method of the IEnumerable and IEnumerable(Of T) interfaces returns an enumerator, which is an object that implements either the System.Collections.IEnumerator or System.Collections.Generic.IEnumerator(Of T) interface, respectively. The IEnumerator and IEnumerator(Of T) interfaces define the methods used by the For Each statement to enumerate the collection.

Implement a private iterator class within the enumerable type that implements ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required