A
Abdessamad Belangour
Hi all,
I have an ArrayList ( phoneBook ) of structures (PhoneEntry's) holding the
name and the owned phone numbers of a person as in below :
--------------------------------------------------------------------------
//------a strcuture holding the name and the phones of a person------
struct PhoneEntry
{ string name,
ArrayList phoneNumbers;
public PhoneEntry(string nm, ArrayList pn)
{ name=nm;
phoneNumbers=pn;
}
}
//------An arraylist of persons-----------
ArrayList phoneBook=new ArrayList(); //already intialized with an other
piece of my program
--------------------------------------------------------------------------
I try then to update the phoneNumbers of every PhoneEntry by looking if it
depends on other entries (Example: if two names are friends then they have
to get the phones of each others).
When i try to enumerate on my phoneBook ArrayList i got the following
Exception :
<< System.InvalidOperationException: Collection was modified; enumeration
operation may not execute.
at System.Collections.ArrayListEnumeratorSimple.MoveNext() >>
I have understood that i can't enumerate on a collection and modify it at
the same time. I have then copied it into another variable, enumerate on the
first and modify the second. but the problem remained the same (as it is the
same reference). How can i force the copy to have an other reference? or
please propose me an other solution.
N.B. The example of code above is fictious and only serves for explanations.
Thanks in advance.
I have an ArrayList ( phoneBook ) of structures (PhoneEntry's) holding the
name and the owned phone numbers of a person as in below :
--------------------------------------------------------------------------
//------a strcuture holding the name and the phones of a person------
struct PhoneEntry
{ string name,
ArrayList phoneNumbers;
public PhoneEntry(string nm, ArrayList pn)
{ name=nm;
phoneNumbers=pn;
}
}
//------An arraylist of persons-----------
ArrayList phoneBook=new ArrayList(); //already intialized with an other
piece of my program
--------------------------------------------------------------------------
I try then to update the phoneNumbers of every PhoneEntry by looking if it
depends on other entries (Example: if two names are friends then they have
to get the phones of each others).
When i try to enumerate on my phoneBook ArrayList i got the following
Exception :
<< System.InvalidOperationException: Collection was modified; enumeration
operation may not execute.
at System.Collections.ArrayListEnumeratorSimple.MoveNext() >>
I have understood that i can't enumerate on a collection and modify it at
the same time. I have then copied it into another variable, enumerate on the
first and modify the second. but the problem remained the same (as it is the
same reference). How can i force the copy to have an other reference? or
please propose me an other solution.
N.B. The example of code above is fictious and only serves for explanations.
Thanks in advance.