469,275 Members | 1,675 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

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
Oct 4 '08 #1
4 33581
shapper <md*****@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 - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 4 '08 #2
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 MyEnumeratorClass : IEnumerable, IEnumerator
{
IEnumerator iEnum;
List<DataStructmyData = new List<DataStruct>();

public MyEnumeratorClass()
{
iEnum= myData.GetEnumerator();
}

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

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
Oct 5 '08 #3
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 MyEnumeratorClass : IEnumerable, IEnumerator
{
IEnumerator iEnum;
List<DataStructmyData = new List<DataStruct>();

public MyEnumeratorClass()
{
iEnum= myData.GetEnumerator();
}

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

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
Oct 5 '08 #4
Duggi <Du***************@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?

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 - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 5 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by elvis_the_king | last post: by
10 posts views Thread by jcc | last post: by
10 posts views Thread by guy | last post: by
1 post views Thread by craigkenisston | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.