Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linked List Enumerator problem

Status
Not open for further replies.

chigley

Programmer
Sep 30, 2002
104
GB
Hello,

I have a linked list (q1) of type Report, where Report is a bespoke class.

I need to enumerate the linked list, and I understand the c#2.0 way of doing this is as follows.

foreach (Report r in q1)
{

}

It compiles ok, but at run time I get an exception thus :

"Collection was modified after the enumerator was instantiated."

I have googled this to death, and the best I could come up with was to add the following to lines to the start of the code block

IEnumerator<Report> e = q1.GetEnumerator();
e.Reset();

Neither e.Reset() nor e.MoveNext() works yet other people say it does. I am confuzzed. Is this a c# bug? Or more likely I'm doing something wrong.

I am coming to c# as a noob, with Java experience. Can anyone help?
 
It was me.

foreach (Report r in q1)
{
if (r.ReportId == reportId)
{
q1.Remove(r);
}

Should have read

foreach (Report r in q1)
{
if (r.ReportId == reportId)
{
q1.Remove(r);
return;
}

Sorry!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top