3

I've made a class called Serie, it's basically a List. How do I make it so that I can make a serie like this; "Serie S = new Serie() { 1, 2, 3, 4 }" like you can do with a List?

    class Serie
{
    public List<decimal> Serie_ { get; set; }

    public decimal this[int index]
    {
        get
        {
            return Serie_[index];
        }
        set
        {
            Serie_.Insert(index, value);
        }
    }

    public Serie()
    {

    }

    public Serie(List<decimal> serie)
    {
        Serie_ = serie;
    }

    public Serie Add(decimal Value)
    {
        List<decimal> lst = new List<decimal>();
        for (int i = 0; i < Serie_.Count; i++)
        {
            lst.Add(Serie_[i]);
        }
        lst.Add(Value);
        Serie S = new Serie(lst);
        return S;
    }

    public double Count()
    {
        return Serie_.Count;
    }

    public static Serie operator +(Serie left, decimal right)
    {
        List<decimal> temp = new List<decimal>();
        for (int i = 0; i < left.Count(); i++)
        {
            temp.Add(left[i] + right);
        }
        return new Serie(temp);
    }
}

When trying to do so I get the error-message;

Cannot initialize type '' with a collection initializer because it does not implement 'System.Collections.IEnumerable'

I do not know how to do so tho.

Tommy R
  • 75
  • 5
  • 5
    Error message answers just that. Implement IEnumerable interface – Andrei Jul 03 '15 at 15:16
  • 1
    possible duplicate of [Implementing a collection initializer for my List wrapper in C#](http://stackoverflow.com/questions/13815593/implementing-a-collection-initializer-for-my-list-wrapper-in-c-sharp) – Chostakovitch Jul 03 '15 at 15:16

3 Answers3

4

Implement IEnumerable<T>, Try this code:

public class Serie : IEnumerable<decimal>
{
    List<decimal> mylist = new List<decimal>();

    public decimal this[int index]
    {
        get { return mylist[index]; }
        set { mylist.Insert(index, value); }
    }

    public IEnumerator<decimal> GetEnumerator()
    {
        return mylist.GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return mylist.GetEnumerator();
    }


    public Serie()
    {

    }

    public Serie(List<decimal> serie)
    {
        mylist = serie;
    }

    public Serie Add(decimal Value)
    {
        mylist.Add(Value);
        return this;
    }

    public double Count()
    {
        return mylist.Count;
    }

    public static Serie operator +(Serie left, decimal right)
    {
        List<decimal> temp = new List<decimal>();
        for (int i = 0; i < left.Count(); i++)
        {
            temp.Add(left[i] + right);
        }
        return new Serie(temp);
    }
}

and also you may need a sample usage of the above class such this:

    static void Main(string[] args)
    {

        Serie s = new Serie();
        s.Add(1).Add(2);
        for (int i = 0; i < s.Count(); i++)
        {
            Console.WriteLine(s[i]);
        }
        Console.ReadLine();
    }
Ali Adlavaran
  • 3,697
  • 2
  • 23
  • 47
  • You'll also need a public `Add` method in order to use collection initializer syntax. – LukeH Jul 03 '15 at 15:23
  • This don't work either; I get a bunch of errors. First off the GetEnumerator method gives the error; "_'[Namespace].Serie' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'. '[Namespace].Serie.GetEnumerator()' cannot implement 'System.Collections.IEnumerable.GetEnumerator()' because it does not have the matching return type of 'System.Collections.IEnumerator'._" And then I get a couple of errors regarding adding a list of decimals to a list of Series in various places. I'm not sure what your trying to do here... I don't know anything about this IEnumerable – Tommy R Jul 03 '15 at 16:47
  • @TommyRäjert , I corrected the answer and i added an example of usage of the that. Please check it :) – Ali Adlavaran Jul 03 '15 at 17:14
1

Technically, all you need is to implement corresponding Add method:

 public class Serie {
   //TODO: add required stuff here

   public void Add(int value) {
     //TODO: add required stuff here
   }
 }

 ...
 // Add method lets you use the syntax sugar 
 Serie S = new Serie() { 1, 2, 3, 4 };

However, since Serie looks like some extension/wrap on List<T> class, it'll be reasonable to implement some interfaces as well, e.g.

  IEnumerable<Decimal>
  IReadOnlyList<Decimal>
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • As far as I know you have to implement `IEnumerable` *and* need a public `Add` method with the correct signature. – Joey Jul 03 '15 at 17:15
0

You must implement IEnumerable<decimal> and implement a method Add(decimal value) - which you have already done.

But viewing over your code, you should rethink, what you want to do. It looks a bit unstructured to me.

Matt
  • 4,612
  • 1
  • 24
  • 44