Question

Below is my class. I need to make it Enumerable. I have looked online and although I have found alot of documentation I am still lost. This I think is absolutely the first time I am going to ask this but could someone please just make this darn thing Enumerable for me and I will figure it out from there. I am using C# ASP.Net 4.0

    public class ProfilePics
{
    public string status { get; set; }
    public string filename { get; set; }
    public bool mainpic { get; set; }
    public string fullurl { get; set; }

}
Was it helpful?

Solution

Well... if all you want is for someone to "just make this darn thing Enumerable", here goes...

public class ProfilePics : System.Collections.IEnumerable
{
    public string status { get; set; }
    public string filename { get; set; }
    public bool mainpic { get; set; }
    public string fullurl { get; set; }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        yield break;
    }
}

It doesn't enumerate anything, but it is enumerable.

Now I'll try to engage in some mind reading and wonder if you want something like this:

public class ProfilePicture
{
    public string Filename { get; set; }
}

public class ProfilePics : IEnumerable<ProfilePicture>
{
    public List<ProfilePicture> Pictures = new List<ProfilePictures>();

    public IEnumerator<ProfilePicture> GetEnumerator()
    {
        foreach (var pic in Pictures)
            yield return pic;
        // or simply "return Pictures.GetEnumerator();" but the above should
        // hopefully be clearer
    }
}

OTHER TIPS

The question is: what do you want to enumerate?

I guess you want some kind of container, containing items of your ProfilePics type so go with

List<ProfilePics>

either inside a class like this:

public class ProfilePic
{
    public string status { get; set; }
    public string filename { get; set; }
    public bool mainpic { get; set; }
    public string fullurl { get; set; }

}

public class ProfilePics : IEnumerable<ProfilePic>
{
    private pics = new List<ProfilePic>();

    // ... implement the IEnumerable members
}

or just by plainly using List<ProfilePics> in those places you need the container.

In case you did miss it: here is the MSDN Docu for this IEnumerable (with some more examples)

To be enumerable class should implement some collection. I don't see any collection property in your example. If you want to make a collectio of profile pics, rename your class to 'ProfiePic' and use List.

If you want to expose some property as collection, make it of type IEnumerable or List or other collection.

I would use a class without inheritance:

using System;
using System.Collections.Generic;

namespace EnumerableClass
{
    public class ProfilePics
    {
        public string status { get; set; }
        public string filename { get; set; }
        public bool mainpic { get; set; }
        public string fullurl { get; set; }

        public IEnumerator<object> GetEnumerator()
        {
            yield return status;
            yield return filename;
            yield return mainpic;
            yield return fullurl;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            ProfilePics pic = new ProfilePics() { filename = "04D2", fullurl = "http://demo.com/04D2", mainpic = false, status = "ok" };
            foreach (object item in pic)
            {
                Console.WriteLine(item);
                //if (item is string) ...
                //else if (item is bool) ...
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top