Solved

Error when looping through a collection

Posted on 2006-07-13
7
281 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
LINQ - C# to VB convertion 12 56
VB.NET Repostiory Pattern 7 35
rebind a grid after user clicks on node in treeview 1 34
Creating a route in asp.net webforms 2 25
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

816 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

14 Experts available now in Live!

Get 1:1 Help Now