473,326 Members | 2,680 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,326 software developers and data experts.

stepping through a collection object in a foreach loop



This is very strange. Say I have code like this. I am simply looping
through a collection object in a foreach loop.

Course course = new Course();

foreach(Student s in course.Students)
{
Console.WriteLine(s.StudentID);
}

When I run the code without debugging, it runs fine. Then when I step
through the loop, I get this error.

An unhandled exception of type 'System.InvalidOperationException'
occurred in mscorlib.dll

Additional information: Collection was modified; enumeration operation
may not execute.
The implmentation of course.Students? Very straightforward.

public ArrayList Students
{
get
{
if (students == null)
students = new ArrayList();

for(int i=0; i<5; i++)
{
Student s = new Student();
s.StudentID = i;
students.Add(s);
}
return students;
}
}

I want Students to be refreshed every time it's called. Now if I do
this (see below), this code will make sure that the Students gets
populated only once. That is not what I want.

What is very strange is that I've been doing this the past 3 years.
Stepped through loops about a trillion types and my implmentation has
always been like the one above.

What I have is a fresh installation of .NET. This is just strange.

public ArrayList Students
{
get
{
if (students == null)
{
students = new ArrayList();

for(int i=0; i<5; i++)
{
Student s = new Student();
s.StudentID = i;
students.Add(s);
}
}
return students;
}
}

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
5 2623
Hi David,
the reason for your error is that while you are looping throught the
Students arraylist i.e.
Course course = new Course();

foreach(Student s in course.Students)
{
Console.WriteLine(s.StudentID);
}
The Students array list was modified which is not allowed. Are you running
this code in a multithreaded environment. Something must have called
course.Students on the same instance of course while another thread was in
the foreach loop.

I would place a lock around the setting and getting of the students
arraylist to make sure it cannot be modified whilst you are looping through
it something like:

public ArrayList Students
{
get
{
if (students == null)
students = new ArrayList();

lock(students.SyncRoot)
{
for(int i=0; i<5; i++)
{
Student s = new Student();
s.StudentID = i;
students.Add(s);
}
}
return students;
}
}

and
Course course = new Course();

lock(course.Students.SyncRoot)
{
foreach(Student s in course.Students)
{
Console.WriteLine(s.StudentID);
}
}

Hope that helps
Mark R Dawson
http://www.markdawson.org


"David C" wrote:


This is very strange. Say I have code like this. I am simply looping
through a collection object in a foreach loop.

Course course = new Course();

foreach(Student s in course.Students)
{
Console.WriteLine(s.StudentID);
}

When I run the code without debugging, it runs fine. Then when I step
through the loop, I get this error.

An unhandled exception of type 'System.InvalidOperationException'
occurred in mscorlib.dll

Additional information: Collection was modified; enumeration operation
may not execute.
The implmentation of course.Students? Very straightforward.

public ArrayList Students
{
get
{
if (students == null)
students = new ArrayList();

for(int i=0; i<5; i++)
{
Student s = new Student();
s.StudentID = i;
students.Add(s);
}
return students;
}
}

I want Students to be refreshed every time it's called. Now if I do
this (see below), this code will make sure that the Students gets
populated only once. That is not what I want.

What is very strange is that I've been doing this the past 3 years.
Stepped through loops about a trillion types and my implmentation has
always been like the one above.

What I have is a fresh installation of .NET. This is just strange.

public ArrayList Students
{
get
{
if (students == null)
{
students = new ArrayList();

for(int i=0; i<5; i++)
{
Student s = new Student();
s.StudentID = i;
students.Add(s);
}
}
return students;
}
}

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #2

David C wrote:
This is very strange. Say I have code like this. I am simply looping
through a collection object in a foreach loop.

Course course = new Course();

foreach(Student s in course.Students)
{
Console.WriteLine(s.StudentID);
}

When I run the code without debugging, it runs fine. Then when I step
through the loop, I get this error.

An unhandled exception of type 'System.InvalidOperationException'
occurred in mscorlib.dll

Additional information: Collection was modified; enumeration operation
may not execute.
The implmentation of course.Students? Very straightforward.

public ArrayList Students
{
get
{
if (students == null)
students = new ArrayList();

for(int i=0; i<5; i++)
{
Student s = new Student();
s.StudentID = i;
students.Add(s);
}
return students;
}
}

I want Students to be refreshed every time it's called. Now if I do
this (see below), this code will make sure that the Students gets
populated only once. That is not what I want.


you are not refreshing anything. every time the get accessor is
called, you add 5 more Student objects to it.

when the debugger is running, in order to display the information in
the debugger, it will invoke the get accessor and cause 5 more objects
to be added to the ArrayList, and foreach loop fails.

Nov 17 '05 #3
Hi,

You cannot modify the collection when you are iterating.

if you want to update the collection elements you would have to implement
the iterator yourself.
you could implement IEnumerator.Current like:

object IEnumerator.Current
{
get
{
//update the student
students[ i].Update(); //or whatever method you use
return students[ i++];
}
}


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"David C" <no*******@nospam.com> wrote in message
news:ea**************@TK2MSFTNGP14.phx.gbl...


This is very strange. Say I have code like this. I am simply looping
through a collection object in a foreach loop.

Course course = new Course();

foreach(Student s in course.Students)
{
Console.WriteLine(s.StudentID);
}

When I run the code without debugging, it runs fine. Then when I step
through the loop, I get this error.

An unhandled exception of type 'System.InvalidOperationException'
occurred in mscorlib.dll

Additional information: Collection was modified; enumeration operation
may not execute.
The implmentation of course.Students? Very straightforward.

public ArrayList Students
{
get
{
if (students == null)
students = new ArrayList();

for(int i=0; i<5; i++)
{
Student s = new Student();
s.StudentID = i;
students.Add(s);
}
return students;
}
}

I want Students to be refreshed every time it's called. Now if I do
this (see below), this code will make sure that the Students gets
populated only once. That is not what I want.

What is very strange is that I've been doing this the past 3 years.
Stepped through loops about a trillion types and my implmentation has
always been like the one above.

What I have is a fresh installation of .NET. This is just strange.

public ArrayList Students
{
get
{
if (students == null)
{
students = new ArrayList();

for(int i=0; i<5; i++)
{
Student s = new Student();
s.StudentID = i;
students.Add(s);
}
}
return students;
}
}

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #4
>>public ArrayList Students
{
get
{
if (students == null)
students = new ArrayList();

for(int i=0; i<5; i++)
{
Student s = new Student();
s.StudentID = i;
students.Add(s);
}
return students;
}
}

and also you shouldn't do things like this in a getter
Nov 17 '05 #5


Thank you all for your replies.

I had a friend run the code with his install of VS.NET 2003, and can
step through this without a problem.

So looks like there is a problem with my instance of VS.NET 2003. I
wouldn't know how to began to address that.

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Iulian Ionescu | last post by:
I have seen the following block of code used by many people to loop through the items in a collection: IEnumerator e; IDisposable disposable1; Object obj1; e = SomeCollection.GetEnumerator();...
2
by: Robert Sentgerath | last post by:
Foreach is a relative handy construct to avoid having to create the classic "for(int loop = 0; loop < collection.Length; loop++) construct. Though when I use foreach instead I do not have access to...
7
by: juli | last post by:
I have strings variables in a collection list and I want to create new collection but to add to it only strings that are distinct (no common strings). For example I have an object sentense which...
2
by: Robert W. | last post by:
I'm trying to write a utility that will use Reflection to examine any data model I pass it and correctly map out this model into a tree structure. When I say "any" , in fact there will only be 3...
13
by: TrintCSD | last post by:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed? foreach(string invoice in...
25
by: David C | last post by:
I posted this question, and from the replies, I get the impression that I worded my posting very poorly, so let me try this again. While debugging and stepping through this foreach loop ...
4
by: John Dalberg | last post by:
I am looking at a problem which is preventing my code to get a reference to any asp control inside a div section which has a runat=server attribute. This can be reproduced in a simple test:...
0
by: Geoffrey | last post by:
Hello, I work with .net remoting,to simplify, when the client connected, he send an handle to an object, the object is used to exchange message and events. no problem. In my server, I keep a...
3
by: gasfusion | last post by:
Hey guys. I'm building an object collection which will be a part of a Data Access Layer i am currently working on. However, i am having some issues iterating through a collection. This is what...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.