0

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; }

}
1
  • What would you want to enumerate? I don't see any obvious data that stands out as a good candidate. Commented Mar 20, 2012 at 5:36

4 Answers 4

2

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
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Whoa!... Did not realise this question was this old. The edit brought it to the main page...
Wholly heck I completely forgot about this question, sad thing is I just went through my list of questions the other day trying to ensure I hadn't forgotten any. Well thank you
0

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)

Comments

0

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.

Comments

0

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) ...
            }
        }
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.