Solved

Error when looping through a collection

Posted on 2006-07-13
7
278 Views
Last Modified: 2012-05-05
I have a generic collection that I iterate through like so:

For Each module As IModule In moduleManager.instance
     '''Do something
Next

In the loop I may or may not remove an item from the collection that I am looping through

So for example if I have one item in the collection and remove it then the loop continues (calling Enumerator.moveNext()) and it crashes with the error:
Collection was modified after the enumerator was instantiated.
 at System.Collection.Generic.LinkedList '1.Enumerator.MoveNext()
......

One way I can get around this is to do:
here:
For Each module As IModule In moduleManager.instance
     '''Do something
     GoTo here
Next

So that if something is removed I just start the loop again.

I don't like using GoTo statements and I have to unnecessarily loop through record that I already have looped through.

What can I do here instead?

Thanks a lot
0
Comment
Question by:se402036se
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 8

Expert Comment

by:doobdave
Comment Utility
hi there,

Instead of using a for...each loop, use a for...next loop based on the count of items in the collection.
Start the loop from the end and work backwards (step -1).
this will allow you to remove records from the collection inside the loop without procuding errors.

Hope to have helped.

Best Regards,
David
0
 
LVL 21

Expert Comment

by:mastoo
Comment Utility
Loop backwards through the collection using an index instead of foreach.
0
 

Author Comment

by:se402036se
Comment Utility
hmmm well the collection is defined as:

Private m_modules As Generic.LinkedList(Of IModule)

How can I loop through this collection using an index?

Something like:
For i As Integer = m_modules.count To 1 Step -1
   'But I cannot do m_modules(i)
Next

as i get the error:
Class 'System.Collections.Generic.LinkedList(Of IModule)' cannot be indexed because it has no default property.

Any ideas?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 21

Expert Comment

by:mastoo
Comment Utility
Ah, I missed the type of collection being used.  I'm still on the old framework so I'd just be guessing, but can't you do your own loop starting with an instance of LinkedListNode and stepping through the loop by calling Next on it?  This way, no iterator is in play so no code complains when you remove values from the linked list.
0
 
LVL 24

Accepted Solution

by:
Jeff Certain earned 125 total points
Comment Utility
You might try this type of approach

    Dim currentModule As LinkedListNode(Of IModule) =  m_modules.First
    Do While Not currentModule Is Nothing
      ' Do something
      currentModule = currentModule .Next
    Loop

** Be careful that you relink all your nodes if you need to delete one
0
 

Author Comment

by:se402036se
Comment Utility
Thanks you gentlemen, I got it with:

 Dim currentModule As Generic.LinkedListNode(Of IModule) = getModuleList.First
        Dim nextModule As Generic.LinkedListNode(Of IModule)

        Do While Not currentModule Is Nothing
            nextModule = currentModule.Next
            'Do something
            currentModule = nextModule
        Loop
0
 
LVL 24

Expert Comment

by:Jeff Certain
Comment Utility
Glad I could help.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now