IEnumerable ... Add items

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

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
 
shapper said:
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<T> or
something similar, that's a different matter.
 
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<DataStruct> myData = 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
 
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<DataStruct> myData = 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
 
Duggi said:
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.
 
Back
Top