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.WriteLi ne(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.Invalid OperationExcept ion'
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 ***