473,406 Members | 2,619 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,406 software developers and data experts.

Collection enumeration exception

I have a collection that I want to edit. When I remove an object from the
collection and then try to enumerate thru it with the foreach statement it
throws an exception - "Collection was modified; enumeration operation may
not execute."

Can somebody tell me how to fix this?
Nov 16 '05 #1
2 2263
Chuck,

Are you removing the item while you are enumerating through it? If so,
then this is not allowed. You need to cycle through the object using the
indexer, or whatever other access method there is to get an item from the
object.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Chuck Bowling" <ch**********@sbcglobal-NO-SPAM.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
I have a collection that I want to edit. When I remove an object from the
collection and then try to enumerate thru it with the foreach statement it
throws an exception - "Collection was modified; enumeration operation may
not execute."

Can somebody tell me how to fix this?

Nov 16 '05 #2
Thanks Nicholas. I feel like an idiot. Forgot the thing was in a big loop...
Whoops... :D

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ev**************@TK2MSFTNGP15.phx.gbl...
Chuck,

Are you removing the item while you are enumerating through it? If so,
then this is not allowed. You need to cycle through the object using the
indexer, or whatever other access method there is to get an item from the
object.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Chuck Bowling" <ch**********@sbcglobal-NO-SPAM.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
I have a collection that I want to edit. When I remove an object from the
collection and then try to enumerate thru it with the foreach statement it
throws an exception - "Collection was modified; enumeration operation may
not execute."

Can somebody tell me how to fix this?


Nov 16 '05 #3

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

Similar topics

3
by: Grandma Wilkerson | last post by:
Hi, The documentation states that enumeration through a collection is inherently NOT thread-safe, since a thread which added/removed an item from said collection could screw up the thread that...
3
by: Anders Both | last post by:
My client-server system, is begining (when more client are connected) sometimes to throw an exception like this: Collection was modified; enumeration operation may not execute. foreach...
11
by: Daniel Billingsley | last post by:
Let's say I'm writing a business app and I want there to be only one instance of the Customer object for each particular customer (representing a database record) being edited. Would it be...
2
by: Tamir Khason | last post by:
I have a queue, wich recieves a lot of messages (between 10 and 100 a second) There is thread that takes care on this queue, but as far as I see it can not follow the messages so "Collection was...
7
by: Franck Diastein | last post by:
Hi, I'm trying to remove items from a collection this way: foreach(Object myO in ObjectCol){ if(myO != "xxx"){ myO.Remove(); } } But I'm having an error telling me that Collection was...
5
by: David C | last post by:
This is very strange. Say I have code like this. I am simply looping through a collection object in a foreach loop. Course course = new Course(); foreach(Student s in course.Students) {...
1
by: Alex Stevens | last post by:
Hi I have a class which inherits the sortedlist collection. I've overloaded the add method to allow the collection to store sqlParameters, with the key being the parameter name. When I try...
3
by: Matt Michael | last post by:
I have a listview control and a collection object right now that I'm trying to pass information to and from. Whenever I click on the checkbox, I want it to remove certain listview items and add...
27
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.