D
David C
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
foreach(Student s in course.Students)
{
Console.WriteLine(s.StudentID);
}
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.
*******************************
Please note the following points
- This code RUNS FINE when run without debugging. NO error
- 2 of my friends can STEP THROUGH the foreach loop without running into
the error message with VS.NET 2003
- 2 other friends get the same error message again with VS.NET 2003
- I used to be able to step through fine with VS.NET 2003. This
instance of VS.NET 2003 is new. Never had a problem stepping through
before on my old machine.
Now let me show you the implentation of the course.Students property.
Please note that following code is just to demonstrate the concept, so
refrain from commenting on my style.
public ArrayList Students
{
get
{
if (students == null)
students = new ArrayList();
--here a call is made to the database to refresh
refreshStudentsFromDataBase();
return students;
}
}
I want it this way because course.Students can change, so everytime it's
called, I want it refreshed.
My understanding is that once course.Students is in a foreach loop, it
will not be called again. And when the code is run without debug, it
confirms my observation. But when I step through, I get the error. 2
people who ran the same code on their machine CAN STEP THROUGH.
Thanks.
I worded my posting very poorly, so let me try this again.
While debugging and stepping through this foreach loop
foreach(Student s in course.Students)
{
Console.WriteLine(s.StudentID);
}
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.
*******************************
Please note the following points
- This code RUNS FINE when run without debugging. NO error
- 2 of my friends can STEP THROUGH the foreach loop without running into
the error message with VS.NET 2003
- 2 other friends get the same error message again with VS.NET 2003
- I used to be able to step through fine with VS.NET 2003. This
instance of VS.NET 2003 is new. Never had a problem stepping through
before on my old machine.
Now let me show you the implentation of the course.Students property.
Please note that following code is just to demonstrate the concept, so
refrain from commenting on my style.
public ArrayList Students
{
get
{
if (students == null)
students = new ArrayList();
--here a call is made to the database to refresh
refreshStudentsFromDataBase();
return students;
}
}
I want it this way because course.Students can change, so everytime it's
called, I want it refreshed.
My understanding is that once course.Students is in a foreach loop, it
will not be called again. And when the code is run without debug, it
confirms my observation. But when I step through, I get the error. 2
people who ran the same code on their machine CAN STEP THROUGH.
Thanks.