CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2007
    Posts
    155

    Iterating a collection that constantly changes

    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?

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    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.

  3. #3
    Join Date
    Mar 2007
    Posts
    155

    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.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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)?

  5. #5
    Join Date
    Mar 2007
    Posts
    155

    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.

    Quote Originally Posted by Arjay View Post
    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 11th, 2012 at 12:56 AM. Reason: Added my answers to Arjay's question

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured