Solved

Error when looping through a collection

Posted on 2006-07-13
7
280 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 125 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

948 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

23 Experts available now in Live!

Get 1:1 Help Now