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

ArrayList behaving badly

Assume fooList is an ArrayList

foreach(string s in fooList) {
if (some condition) {
fooList.Remove(s);
}
}
I get the following runtime error:

An unhandled exception of type 'System.InvalidOperationException' occurred
in mscorlib.dll
Additional information: Collection was modified; enumeration operation may
not execute.
How can I fix this?
Thanks
Nov 16 '05 #1
5 1857
Keith,

you cannot do what you want in this manner.
the foreach statement operates based on the GetEnumerator() reply, and
therefor when you change the content of the arraylist the enumerator is no
longer valid.

try doing it in an other way.

maybe: create a second arraylist for deleted items:

<code>
ArrayList recycleBin = new ArrayList();
foreach (string s in fooList)
{
if (some condition)
{
recycleBin.Add(s);
}
}

foreach (string s in recycleBin)
{
fooList.Remove(s);
}
</code>

HTH
Picho
"Keith O" <ke******@hotmail.com> wrote in message
news:RL********************@rogers.com...
Assume fooList is an ArrayList

foreach(string s in fooList) {
if (some condition) {
fooList.Remove(s);
}
}
I get the following runtime error:

An unhandled exception of type 'System.InvalidOperationException' occurred
in mscorlib.dll
Additional information: Collection was modified; enumeration operation may
not execute.
How can I fix this?
Thanks

Nov 16 '05 #2
Within the foreach you are modifying the arraylist. That shouldn't be done.

- Sahil Malik
http://codebetter.com/blogs/sahil.malik/


"Keith O" <ke******@hotmail.com> wrote in message
news:RL********************@rogers.com...
Assume fooList is an ArrayList

foreach(string s in fooList) {
if (some condition) {
fooList.Remove(s);
}
}
I get the following runtime error:

An unhandled exception of type 'System.InvalidOperationException' occurred
in mscorlib.dll
Additional information: Collection was modified; enumeration operation may
not execute.
How can I fix this?
Thanks

Nov 16 '05 #3

"Keith O" <ke******@hotmail.com> wrote in message
news:RL********************@rogers.com...
Assume fooList is an ArrayList
foreach(string s in fooList) {
if (some condition) {
fooList.Remove(s);
}
}


Or you could use a for-loop and traverse the list backwards.

- Michael S
Nov 16 '05 #4
you can use an index for loop rather then the foreach loop.
On Mon, 14 Feb 2005 01:28:56 -0500, "Keith O" <ke******@hotmail.com>
wrote:
Assume fooList is an ArrayList

foreach(string s in fooList) {
if (some condition) {
fooList.Remove(s);
}
}
I get the following runtime error:

An unhandled exception of type 'System.InvalidOperationException' occurred
in mscorlib.dll
Additional information: Collection was modified; enumeration operation may
not execute.
How can I fix this?
Thanks


Nov 16 '05 #5
You can subvert the read-only characteristics of the foreach construct using
this code

foreach(string s in new System.Collection.ArrayList(fooList)) { //this
opening brace should be on
//the next line by the way
if (some condition) {
fooList.Remove(s);
}
}

Is it a recommended approach? I sincerely doubt it but that's a question for
another thread?

--
Regards,
Alvin Bruney [Microsoft MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://www.lulu.com/owc
----------------------------------------------------------
"Keith O" <ke******@hotmail.com> wrote in message
news:RL********************@rogers.com...
Assume fooList is an ArrayList

foreach(string s in fooList) {
if (some condition) {
fooList.Remove(s);
}
}
I get the following runtime error:

An unhandled exception of type 'System.InvalidOperationException' occurred
in mscorlib.dll
Additional information: Collection was modified; enumeration operation may
not execute.
How can I fix this?
Thanks

Nov 16 '05 #6

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

Similar topics

4
by: Drew Lettington | last post by:
I'm making a simple call to display error messages in a MessageBox from a Windows form and the MessageBox is not behaving in a modal fashion. My modal form displays, the user clicks a button and...
10
by: Garmt de Vries | last post by:
In an overview of book titles in many languages, which I wrote about in another thread, I've run across a problem with some titles in scripts that are written from right to left, like Arabic and...
6
by: Bryon | last post by:
I need to sort an ArrayList of objects. I am unable to find a method for this. DO I need to role my own sorting? Or is there something like the qsort() function in C of old??? Thanks
0
by: elime | last post by:
Hi all I have a strange behaving on some PC with my DataGrid. It only occurs on some PC, on others it works perfectly fine. ->(same ..net version installed) it's very confusing. starting...
11
by: The Crow | last post by:
i have a arraylist. say it contains integer values. i want to be able to inform user, which indexes in the array contain same values. but there can be N different values, and M different indexes...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
3
by: David Vincent | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hello I'm hoping to get some insight into a situation that seems odd to me. My Python experience is limited; I've just started using the unittest module....
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
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...

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.