IEnumerable ... Add items

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shapper

    IEnumerable ... Add items

    Hi,

    I have the following IEnumerable:

    IEnumerable targets;

    I want to add items to it where each item is as follows:

    new { Name = "Book", Author = "John" }

    I am just having a few problems in adding the items.

    How is it done?

    Thanks,
    Miguel
  • Jon Skeet [C# MVP]

    #2
    Re: IEnumerable ... Add items

    shapper <mdmoura@gmail. comwrote:
    I have the following IEnumerable:
    >
    IEnumerable targets;
    >
    I want to add items to it where each item is as follows:
    >
    new { Name = "Book", Author = "John" }
    >
    I am just having a few problems in adding the items.
    >
    How is it done?
    You can't add items to an arbitrary IEnumerable or IEnumerable<T- it
    only offers an interface to read items. If it's actually a List<Tor
    something similar, that's a different matter.

    --
    Jon Skeet - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon.skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • Duggi

      #3
      Re: IEnumerable ... Add items

      On Oct 4, 4:27 am, shapper <mdmo...@gmail. comwrote:
      Hi,
      >
      I have the following IEnumerable:
      >
      IEnumerable targets;
      >
      I want to add items to it where each item is as follows:
      >
      new { Name = "Book", Author = "John" }
      >
      I am just having a few problems in adding the items.
      >
      How is it done?
      >
      Thanks,
      Miguel
      See the sample code below

      class DataStruct
      {
      public string Name { get; set; }
      public string Author { get; set; }
      }

      class MyEnumeratorCla ss : IEnumerable, IEnumerator
      {
      IEnumerator iEnum;
      List<DataStruct myData = new List<DataStruct >();

      public MyEnumeratorCla ss()
      {
      iEnum= myData.GetEnume rator();
      }

      public IEnumerator GetEnumerator()
      {
      return myData.GetEnume rator();
      }

      public void Reset()
      {
      iEnum.Reset();
      }
      public bool MoveNext()
      {
      return iEnum.MoveNext( );
      }
      public object Current
      {
      get
      {
      return iEnum.Current;
      }
      }
      }

      you can also refer to http://www.codeproject.com/KB/cs/sssienumerable.aspx

      -Cnu

      Comment

      • Duggi

        #4
        Re: IEnumerable ... Add items

        On Oct 4, 4:27 am, shapper <mdmo...@gmail. comwrote:
        Hi,
        >
        I have the following IEnumerable:
        >
        IEnumerable targets;
        >
        I want to add items to it where each item is as follows:
        >
        new { Name = "Book", Author = "John" }
        >
        I am just having a few problems in adding the items.
        >
        How is it done?
        >
        Thanks,
        Miguel
        Following is with ADD

        class DataStruct
        {
        public string Name { get; set; }
        public string Author { get; set; }
        }

        class MyEnumeratorCla ss : IEnumerable, IEnumerator
        {
        IEnumerator iEnum;
        List<DataStruct myData = new List<DataStruct >();

        public MyEnumeratorCla ss()
        {
        iEnum= myData.GetEnume rator();
        }

        public IEnumerator GetEnumerator()
        {
        return myData.GetEnume rator();
        }

        public void Reset()
        {
        iEnum.Reset();
        }
        public bool MoveNext()
        {
        return iEnum.MoveNext( );
        }
        public object Current
        {
        get
        {
        return iEnum.Current;
        }
        }

        public void ADD(DataStruct ds)
        {
        myData.Add(ds);
        }
        }


        I hope this is what you are trying to achieve...

        -Cnu

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: IEnumerable ... Add items

          Duggi <DuggiSrinivasa Rao@gmail.comwr ote:
          I have the following IEnumerable:

          IEnumerable targets;

          I want to add items to it where each item is as follows:

          new { Name = "Book", Author = "John" }

          I am just having a few problems in adding the items.

          How is it done?
          >
          Following is with ADD
          That doesn't help the OP if he only has an IEnumerable or
          IEnumerable<T>.

          Now you could create an IEnumerable which would return the existing
          data and then the new item, but it's not actually adding to the
          original data source.

          --
          Jon Skeet - <skeet@pobox.co m>
          Web site: http://www.pobox.com/~skeet
          Blog: http://www.msmvps.com/jon.skeet
          C# in Depth: http://csharpindepth.com

          Comment

          Working...