PC Review


Reply
Thread Tools Rate Thread

ArrayList behaving badly

 
 
Keith O
Guest
Posts: n/a
 
      14th Feb 2005
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


 
Reply With Quote
 
 
 
 
Picho
Guest
Posts: n/a
 
      14th Feb 2005
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" <(E-Mail Removed)> wrote in message
news:RLKdneKWvd662I3fRVn-(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Sahil Malik
Guest
Posts: n/a
 
      14th Feb 2005
Within the foreach you are modifying the arraylist. That shouldn't be done.

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




"Keith O" <(E-Mail Removed)> wrote in message
news:RLKdneKWvd662I3fRVn-(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Michael S
Guest
Posts: n/a
 
      14th Feb 2005

"Keith O" <(E-Mail Removed)> wrote in message
news:RLKdneKWvd662I3fRVn-(E-Mail Removed)...
> 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


 
Reply With Quote
 
qwerty
Guest
Posts: n/a
 
      14th Feb 2005
you can use an index for loop rather then the foreach loop.


On Mon, 14 Feb 2005 01:28:56 -0500, "Keith O" <(E-Mail Removed)>
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
>


 
Reply With Quote
 
Alvin Bruney [MVP]
Guest
Posts: n/a
 
      14th Feb 2005
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" <(E-Mail Removed)> wrote in message
news:RLKdneKWvd662I3fRVn-(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PC Behaving badly OhioGuy DIY PC 7 19th Dec 2007 02:10 PM
PC Behaving badly OhioGuy DIY PC 2 18th Dec 2007 03:42 PM
Excel behaving badly in ppt =?Utf-8?B?UmFtaW4=?= Microsoft Powerpoint 12 11th Apr 2006 06:00 PM
Excel behaving badly no_name Microsoft Excel Discussion 1 23rd Dec 2005 10:44 AM
IE windows behaving badly in XP finleymon Windows XP Networking 0 8th Oct 2003 04:04 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:41 PM.