473,386 Members | 1,741 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,386 software developers and data experts.

InvalidOperationException: Please Help

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.MoveN ext() >>
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.

Nov 15 '05 #1
4 1639

"Abdessamad Belangour" <be*******@irin.univ-nantes.fr> wrote in message
news:ug****************@tk2msftngp13.phx.gbl...
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.MoveN ext() >>
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
Nov 15 '05 #2
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.

"Eric Eggermann >" <<none> a écrit dans le message de
news:uW**************@TK2MSFTNGP11.phx.gbl...

"Abdessamad Belangour" <be*******@irin.univ-nantes.fr> wrote in message
news:ug****************@tk2msftngp13.phx.gbl...
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.MoveN ext() >>
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

Nov 15 '05 #3

"Abdessamad Belangour" <be*******@irin.univ-nantes.fr> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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
Nov 15 '05 #4
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 >" <<none> a écrit dans le message de
news:%2****************@TK2MSFTNGP11.phx.gbl...

"Abdessamad Belangour" <be*******@irin.univ-nantes.fr> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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

Nov 15 '05 #5

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

Similar topics

0
by: kiplring | last post by:
I'm using managed directx. I made a KeyFrameInterpolator object. And get a exception above. I think it is because GC reconize KeyFrameInterpolator object as Pinned object. So I added next line....
0
by: LJ | last post by:
I have been getting this message and can't resolve the problem. I have checked the AntiVirus software and added the application directory to the exculsion list. The application is on a web farm,...
6
by: Tony | last post by:
Dear All, When I run an example program on the http://www.dotnetjunkies.com/quickstart/util/srcview.aspx?path=/quickstart/aspplus/samples/webforms/data/datagrid1.src&file=VB\datagrid1.aspx&font=3 ...
1
by: Trygve Lorentzen | last post by:
Hi, my webservice is running on Win2000 SP4, IIS 5.0 fully patched, connecting to a MySQL database and mainly returning Typed DataSet's from webmethods. After running for a while, generally a...
3
by: Joseph Geretz | last post by:
System.InvalidOperationException: WebServiceBindingAttribute is required on proxy classes. My environment: Visual Studio 2005, targeting FX 2.0; I've developed a Web Service which uses DIME to...
2
by: Ian Toltz | last post by:
I've got a form element (a wizard, actually) which is giving me problems when I try to invoke some of its methods from one of the methods of a filesystemwatcher. Basically the watcher sits there...
4
by: J055 | last post by:
Hi I get the error: System.InvalidOperationException: The ReadElementContentAsString method is not supported on node type None. Line 267, position 12. when running the following code: ...
2
by: Jeroen | last post by:
We are experiencing a tuff-to-debug problem ever since we introduced a WebBrowser control into our failry large application. I'm not sure if the problem description below contains enough details,...
0
by: para15000 | last post by:
Hello I have a C# desktop application in which one thread that I create continously gets an image from a source(it's a digital camera actually) and puts it on a panel(panel.Image = img) in the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.