473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 34006
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: elvis_the_king | last post by:
Hi all, I am asking myself wheter IEnumerable has any inherent semantics? The docs seem to be very unclear to me. More specifically: When using a "foreach(...)" construct to enumerate a...
10
by: jcc | last post by:
Hi guys, I'm a newbie to C#. My Visual Studio 2005 failed to compile the following code with error as 'HelloWorld.A' does not implement interface member...
1
by: Joe | last post by:
I have a class that is being returned by several WebMethods. Ok - all is good. Now if I have this class implement IEnumerable, the data is no longer serialized. This class does contain a...
10
by: guy | last post by:
why is it that some collection classes, eg BindingList, but others as well do not expose their Items property? It is there as a Protected property so why not make it public? I can work around...
1
by: craigkenisston | last post by:
I'm working on someone else code I don't quite understand it. It has a class with a method like : private IEnumerable<ProductGetProducts(int count) { blah, blah } And I need to get an...
13
by: David | last post by:
Hi all, I have a singleton ienumerable that collects data from a database. I have listed the code below. It has the usual methods of current, move and reset. I need to go to a certain position...
12
by: gnewsgroup | last post by:
I've read the msdn doc about IEnumerable. It seems to me that IEnumerable objects are essentially wrapped-up arrays. It simply gives us the foreach convenience. Is this correct?
0
by: Weeble | last post by:
On Jun 20, 11:10 pm, Göran Andersson <gu...@guffa.comwrote: I did it that way because CopyTo has a whole bunch of failure cases to take care of (multi-dimensional array, not enough space,...
2
by: cmrhema | last post by:
Hi, I have a dataset that has two datatables. In the first datatable I have EmpNo,EmpName and EmpAddress In the second datatable I have Empno,EmpJoindate, EmpSalary. I want a result where...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.