none
How do you create a new IEnumerable<Collection<String>> and fill in the collection?

    Question

  • I want to build a collection of type System.Collection.ObjectModel.Collection<System.String>.  Lets call this ‘myColl’.

     

    This has to be enumerable so I need to do:

    System.Collection.Generic.IEnumerable<myColl>. 

     

    How do I put items into this collection? If you try to do myEnum.Add or something like that, there is not addition mechanism like there is for just a collection. Also, if I create my collection, then create a new IEnumerable<Collection<String>> test = myColl as IEnumerable<Collection<String>>;, I end up with a null 'test' value.

    Thanks!


    EM
    Sunday, December 27, 2009 9:56 PM

Answers

  • So what I finally figured out was that what I need was an IEnumerable<string> so I did this:

    Collection<String> myColl = new Collection<String>();
    myColl.Add("John");
    myColl.Add("George");

    IEnumerable<String> test = myColl as IEnumerable<String>;

    This works like I expected.

    Thanks!
    EM
    • Proposed as answer by SamAgain Tuesday, December 29, 2009 8:28 AM
    • Marked as answer by eryang Tuesday, January 05, 2010 3:16 AM
    Monday, December 28, 2009 6:24 PM

All replies

  • You cannot declare an instance of IEnumerable - it is an interface.  You need to instantiate an instance of something that derives from IEnumerable (Most everything in the Collections namespace and a few other assorted primitive types at System level).  From there, if the need persists - you can cast the collection as an IEnumerable and access the members defined by the interface.
    Monday, December 28, 2009 1:18 AM
  • You cannot declare an instance of IEnumerable - it is an interface.
    Technically, you can. You can then instantiate it to any class that implements same interface. That is Polymorphism.

    Etowah_man,

    here's how you could do it:

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                IEnumerable objMyColl = default(IEnumerable); 
                string[] tempArray = { "one", "two", "three", "four" };
                objMyColl = new myColl(tempArray);
    
                foreach (string Str in objMyColl)
                {
                    //For each implements getEnumerator method
                    MessageBox.Show(Str);
                }
            }
    
            public class myColl : IEnumerable
            {
    
                private string[] _data;
    
                public myColl(string[] strArray)
                {
                    _data = strArray;
                }
    
                public System.Collections.IEnumerator GetEnumerator()
                {
                    return new myEnum(_data);
                }          
            }
            public class myEnum : IEnumerator
            {
                int position = -1;
                string[] values;
    
                public myEnum(string[] strArray)
                {
                    values = strArray;
                }
    
                public object Current
                {
                    get
                    {
                        try
                        {
                            return values[position];
                        }
                        catch (IndexOutOfRangeException ex)
                        {
                            throw new InvalidOperationException();
                        }
                    }
                }
    
                public bool MoveNext()
                {
                    position += 1;
                    return (position < values.Length);
                }
    
                public void Reset()
                {
                    position = -1;
                }
            }
    
        }


    Thanks

    My BlogMy Facebook
    • Proposed as answer by Rudedog2MVP Tuesday, December 29, 2009 2:14 PM
    Monday, December 28, 2009 7:09 AM
  • Syntaxeater is correct in saying that an instance of IEnumerable is not possible because it is an interface.  Interfaces do not generate instances.  No technicality exists that contradict this.  What you can have, again as Syntaxeater explained, is classes that implement the interface.

    If what the OP want is a collection of collection of strings, then I don't know what is stopping him.

    A quick way would be:

    List<List<string>> myColl = new List<List<string>>();
    myColl.Add(new List<string>());
    myColl[0].Add("A new string");
    

    Or something similar to this.  The object pointed to by the variable myColl can be used in places where an IEnumerable<> pointer is needed.
    MCP
    Monday, December 28, 2009 8:24 AM

  • hmm, my fault. I misunderstood. Sorry.
    Thanks

    My BlogMy Facebook
    Monday, December 28, 2009 8:37 AM
  • So what I finally figured out was that what I need was an IEnumerable<string> so I did this:

    Collection<String> myColl = new Collection<String>();
    myColl.Add("John");
    myColl.Add("George");

    IEnumerable<String> test = myColl as IEnumerable<String>;

    This works like I expected.

    Thanks!
    EM
    • Proposed as answer by SamAgain Tuesday, December 29, 2009 8:28 AM
    • Marked as answer by eryang Tuesday, January 05, 2010 3:16 AM
    Monday, December 28, 2009 6:24 PM
  • Make Omie's class "myEnum" nested inside of "myColl", and everything still works.  Try it.

    I would nest the class because it serves no other classes except for myColl.

    Mark the best replies as answers. "Fooling computers since 1971."
    Monday, December 28, 2009 6:36 PM
  • Well, not that I'm arguing that Omies way isn't the right way, but when I look at the amount of code I wrote (and it works) compared to the amount of code that Omie wrote, I'd have to say I'd chose my route.

    Again, what I ended up needing wasn't as complex as what I thought I originally needed and therefore the code was more simple.

    Thanks!
    EM
    Monday, December 28, 2009 10:43 PM
  • Well, not that I'm arguing that Omies way isn't the right way, but when I look at the amount of code I wrote (and it works) compared to the amount of code that Omie wrote, I'd have to say I'd chose my route.

    Again, what I ended up needing wasn't as complex as what I thought I originally needed and therefore the code was more simple.

    Thanks!
    EM


    Bad move.  The easy way out.

    There is no substitute for thorough programming.

    Most common mistake made by developers is not re-writing bad or poorly written code.

    Eventually, most people learn that it is better to re-write at the earliest possible point in developement.

    Bad code has a tendency to come back to haunt you, and then force you to write even more bad code.

    Mark the best replies as answers. "Fooling computers since 1971."
    Tuesday, December 29, 2009 2:14 PM