I want iterate a collection, but before I finish to iterate, the collection is modified.
So, whenever the collection is modified, I want to re-iterate the collection from the beginning again.
Any inputs on how will I achieve this?
Re: Iterating a collection that constantly changes
What kind of collection, specifically? List? Dictionary?
Best Regards,
BioPhysEngr http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
Re: Iterating a collection that constantly changes
It's a collection from a library. It implemented CollectionBase, which in-turn implemented IList, ICollection, IEnumerable. I've no control over the library as to when the collection will be modified.
Re: Iterating a collection that constantly changes
Can you clarify how you propose to handle notifications when the collection changes in the middle of your iteration? Are you sending an event that indicatates a change? Does the enumerator return a copy of the collection (or does it return a reference to the actual underlying collection)?
Re: Iterating a collection that constantly changes
Thank you BioPhyEngr and Arjay.
Let's say the original collection has a, b, c.
a, b, c - auto created
I want to manually create same number of elements as in the collection
d, e, f - manually created
After that, I want to add it to the original collection. In the end, it will be-
a, b, c, d, e, f
The framework will notify me whenver a, b, c is changed. The d, e, f I created is dependent on values of a, b, c. The problem I face is in creation of d, e, f. To create d, e, f I want to iterate the original collection, but while I was iterating, the collection was modified.
In short I want to make sure the original collection will always have same number of manually created elements.
Originally Posted by Arjay
Can you clarify how you propose to handle notifications when the collection changes in the middle of your iteration? Are you sending an event that indicatates a change? Does the enumerator return a copy of the collection (or does it return a reference to the actual underlying collection)?
I'll not send any notification. I just want to make sure the collection have same number of my custom created elements.
Does the enumerator return a copy of the collection (or does it return a reference to the actual underlying collection)?
It's sending the actual underlying collection. But I can use .ToArray() if I need a copy.
Last edited by thomus07; December 10th, 2012 at 11:56 PM.
Reason: Added my answers to Arjay's question
Bookmarks