SolutionCottage.com
Posted  14 Dec 2008 - 21:35:57

Category: ASP.net


Make your custom class support IEnumerable

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

Solution:
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




0 comments:








© SolutionCottage 2008-2011 9/26/2017 10:12:49 AM