InvalidOperationException: Please Help

  • Thread starter Thread starter Abdessamad Belangour
  • Start date Start date
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.
 
Abdessamad Belangour said:
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.


Hello, Abdessamad
Someone else may well think of a better way, but have you tried using just a
for loop instead of foreach?

Anyway, copying an object so the reference is different is called a deep
copy. What you did, when you copied the object is called a shallow copy. One
way to do a deep copy is to serialize the object out to a file, and then
deserialize it into your new variable. I'm using this rather alot in my
current project, works great.
Look at these MSDN magazine articles by Jeffrey Richter. I got them from
someone in this group a couple months ago.The first part of the article
explains how to do a deep copy.
http://tinyurl.com/ywrkw
http://tinyurl.com/2txhx
http://tinyurl.com/yt693

HTH,
Eric
 
Hi Eric,
Thank you twice. The first is for the "For loop" because it solves the
problem. The second is for the information of deep copy because i will need
it later too.
However i would like to ask you if you ever have encounter the problem of (=
=) operator or Object.Equals method.
During my search in an array of object for a given object, the Object.Equals
method gives false even for two objects with the same data. I guess that
ther's a problem of references again. Have you any idea on how to solve this
problem?
Thanks again.
Abdessamad.
 
snip...
However i would like to ask you if you ever have encounter the problem of (=
=) operator or Object.Equals method.
During my search in an array of object for a given object, the Object.Equals
method gives false even for two objects with the same data. I guess that
ther's a problem of references again. Have you any idea on how to solve this
problem?
Thanks again.
Abdessamad.

Object.Equals is a reference comparison. Two seperate objects return false
even if the data is the same. You need to override the equals method in the
object. This will cause lookups like in collections and things to return the
proper object based on whatever you want, and not just reference equality.
It's funny, but I just did this today. The code for my object looks like
this,

public override bool Equals(object PaperType)
{
//If the references are the same....
if(base.Equals(PaperType){ return true; }

//otherwise compare the fields
DEPaperType pt;
try
{
//cast object to our type
pt = (DEPaperType)PaperType;
}
catch(System.InvalidCastException)
{
//if it didn't work, they can't be equal.
return false;
}
//Test each field, if it's wrong, just return and don't continue.
if([PaperType.field] != [this.field]){ return false; }
...
...
...
//if we get here, we've passed all the tests, so consider the objects as
equal
return true;
}

HTH
Eric
 
The Equals method you have defined is proper to specific objects. What about
future needs of comparing other objects?. In my opinion we need comparing
fields in a more general way and probably this can be done through
Reflection.We call GetFields Method of Type Object. and then we compare
field by field.
What do you yhink ?



Moreover comparing field by field is also
comparing object like this is in my opinion very costable.
I think that overriding the the
Eric Eggermann > said:
snip...
However i would like to ask you if you ever have encounter the problem
of
(=
=) operator or Object.Equals method.
During my search in an array of object for a given object, the Object.Equals
method gives false even for two objects with the same data. I guess that
ther's a problem of references again. Have you any idea on how to solve this
problem?
Thanks again.
Abdessamad.

Object.Equals is a reference comparison. Two seperate objects return false
even if the data is the same. You need to override the equals method in the
object. This will cause lookups like in collections and things to return the
proper object based on whatever you want, and not just reference equality.
It's funny, but I just did this today. The code for my object looks like
this,

public override bool Equals(object PaperType)
{
//If the references are the same....
if(base.Equals(PaperType){ return true; }

//otherwise compare the fields
DEPaperType pt;
try
{
//cast object to our type
pt = (DEPaperType)PaperType;
}
catch(System.InvalidCastException)
{
//if it didn't work, they can't be equal.
return false;
}
//Test each field, if it's wrong, just return and don't continue.
if([PaperType.field] != [this.field]){ return false; }
...
...
...
//if we get here, we've passed all the tests, so consider the objects as
equal
return true;
}

HTH
Eric
 
Back
Top