Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Error when looping through a collection

Posted on 2006-07-13
7
Medium Priority
?
288 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
ID: 17100282
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
ID: 17100284
Loop backwards through the collection using an index instead of foreach.
0
 

Author Comment

by:se402036se
ID: 17100740
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 21

Expert Comment

by:mastoo
ID: 17101154
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 500 total points
ID: 17102221
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
ID: 17107943
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
ID: 17107988
Glad I could help.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month10 days, 15 hours left to enroll

885 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