473,756 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

InvalidOperatio nException: 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(stri ng 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.InvalidO perationExcepti on: Collection was modified; enumeration
operation may not execute.
at System.Collecti ons.ArrayListEn umeratorSimple. 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.

Nov 15 '05 #1
4 1661

"Abdessamad Belangour" <be*******@irin .univ-nantes.fr> wrote in message
news:ug******** ********@tk2msf tngp13.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(stri ng 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.InvalidO perationExcepti on: Collection was modified; enumeration
operation may not execute.
at System.Collecti ons.ArrayListEn umeratorSimple. 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
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******** ******@TK2MSFTN GP11.phx.gbl...

"Abdessamad Belangour" <be*******@irin .univ-nantes.fr> wrote in message
news:ug******** ********@tk2msf tngp13.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(stri ng 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.InvalidO perationExcepti on: Collection was modified; enumeration operation may not execute.
at System.Collecti ons.ArrayListEn umeratorSimple. 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

Nov 15 '05 #3

"Abdessamad Belangour" <be*******@irin .univ-nantes.fr> wrote in message
news:%2******** ********@TK2MSF TNGP11.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)Pa perType;
}
catch(System.In validCastExcept ion)
{
//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******** ********@TK2MSF TNGP11.phx.gbl. ..

"Abdessamad Belangour" <be*******@irin .univ-nantes.fr> wrote in message
news:%2******** ********@TK2MSF TNGP11.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)Pa perType;
}
catch(System.In validCastExcept ion)
{
//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
1717
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. --------- System.Runtime.InteropServices.GCHandle.Alloc(myKeyFrameInterpolator,System.Runtime.InteropServices.GCHandleType.Normal); --------- I set it as Normal. But I got the same Exception. I don't know what the Handle means.(Handle is not...
0
2169
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, using SANS for file storage. It seemed to start once we installed the SSL cert. But, that could be just when we noticed the problem. Any ideas would be greatly appreciated. Thanks <!ljr!>
6
6116
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 I received an error message: "The ConnectionString property has not been initialized.
1
6202
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 few days, the webservice stops responding and the .NET windows app client fails with no informative error message. When I try to run any webmethod from the webservice locally in the browser I first get a "This page cannot be display IE error...
3
14225
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 transfer file attachments to and from the server. I'm using WSE 2.0 SP3 which is supposed to support FX 2.0, as far as I know. (We will soon move on to MTOM, however I'm stuck with DIME for now; the solution was originally written for FX 1.1, and I...
2
1472
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 waiting to see the file and then it's supposed to go to the next page (wizard1.Next()). I get an InvalidOperationException when I do this, though, and as near as I can tell it's because the watcher creates a new thread and it's trying to access...
4
12139
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: while (reader.Read()) {
2
4310
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, so if I need to elaborate on something please ask for it. We have a UserControl with a WebBrowser on it. This UserControl is instantiated a few times during the program run, depending on user interaction. The WebBrowser then navigates to a local...
0
2328
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 GUI(which must be another thread as it is the code-behind of a control. The application works but on some machines I get the following error at random time
0
9431
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9255
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9844
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9819
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9689
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8688
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6514
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5119
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3780
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.