Posted  14 Dec 2008 - 21:35:57


Make your custom class support IEnumerable

You might want to search through a collection of your objects using foreach but get a message that this class is not supporting IEnumerable.

Make your class inherit the IEnumerable interface, doing something like this:

public class AlbumImages : IEnumerable

Then say you have AlbumImages with Images in of a type AlbumImage. Apply this implementation of GetEnumerator:

public IEnumerator GetEnumerator() { return (Images as IEnumerable).GetEnumerator(); }

In this example, Images is a List of type AlbumImage. This will now enable you to use the code:

AlbumImages imagesCollection = new AlbumImages(); foreach (AlbumImage image in imagesCollection ) { }

Marked as helpful by 1 user


© SolutionCottage 2008-2011 7/21/2017 3:32:29 AM