What I have done in the past when deleting is load the objects into a seperate collection, then perform the action when done enumerating. What I did was for deleting but I am sure the same approach will work for adding new objects
for each something in colOfSomethings If something.isOld then Add to DeleteColl next for each something in DeleteColl colOfSomethings.remove(something) next -----Original Message----- From: Marsh, Drew [mailto:[EMAIL PROTECTED]] Sent: Monday, June 10, 2002 9:18 AM To: [EMAIL PROTECTED] Subject: Re: [DOTNET] enumerating a collection while adding more items to it Scott Watermasysk [mailto:[EMAIL PROTECTED]] wrote: > Is it possible to enumerate a collection while still adding > new items to it? One of my applications needs to populate a > collection, then I need to walk through this collection > possibly adding new items to the collection until no new > items are found. (I am certain this will not enter an > infinite loop). Is this possible? Is there a better way to > approach this? >From the SDK docs on IEnumerator[1]: <snip> An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying or deleting elements, the enumerator is irrecoverably invalidated and the next call to MoveNext or Reset throws an InvalidOperationException. If the collection is modified between MoveNext and Current, Current will return the element that it is set to, even if the enumerator is already invalidated. </snip> HTH, Drew .NET MVP [1] http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/ frlrfSystemCollectionsIEnumeratorClassTopic.asp OR ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemCollectionsIEnumeratorClas sTopic.htm You can read messages from the DOTNET archive, unsubscribe from DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com. You can read messages from the DOTNET archive, unsubscribe from DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.