Solved

Collection Error - Collection was modified; enumeration operation may not execute

Posted on 2006-06-26
4
285 Views
Last Modified: 2008-01-09
I’ve got an arraylist which I use extensively throughout my program. There are areas that add to the list and areas that amend items in the list. But I keep getting this error ‘Collection was modified; enumeration operation may not execute’. I sort of understand the reason for the error, but can’t find a way around it.

I need iterate through the array and amend data in the array where needed.

So what's the best way to update an item in an arraylist without getting the error ?

I’ve tried assigning the arraylist item as :   arrData(1) = “x”
And also tried removing the item and re-adding (not the best)

This is a back to basics question I know. But I can’t find any literature on it.
0
Comment
Question by:Kinger247
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 9

Expert Comment

by:lojk
ID: 16983148
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref2/html/P_System_Collections_ArrayList_SyncRoot.htm

Dim myCollection As New ArrayList()
 Dim item As Object
 SyncLock myCollection.SyncRoot
  For Each item In myCollection
  ' Insert your code here.
  Next item
 End SyncLock

will lock the collection to other  code whilst you are modifying/reading it

0
 
LVL 10

Author Comment

by:Kinger247
ID: 16983604
I see, but how do you loop through an array and amend its items ?
I've created a small app below, which is sort of what I'm trying to do. But after the first item update, the code fails on the 'next' statement with the ‘Collection was modified; enumeration operation may not execute’ error, then misses out on the other items.

'// CODE ---------------------------------------

    Private Structure Struct
        Dim Name As String
        Dim Age As Integer
    End Structure

    Private Array As New ArrayList

    Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
        Dim Struct As Struct = Nothing
        Dim i As Integer = 0

        Try

            For Each Struct In Array
                i += 1

                With Struct
                    .Age += 1
                End With

                Array(i) = Struct
            Next

        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Sub
0
 
LVL 21

Accepted Solution

by:
mastoo earned 300 total points
ID: 16984292
Just like your example, except use an index instead of foreach.
0
 
LVL 34

Assisted Solution

by:Sancler
Sancler earned 200 total points
ID: 16984341
You're mixing up two approaches

    Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
        Dim Struct As Struct = Nothing
        Dim i As Integer = 0

        Try

            'Approach using INDEX
            For i = 0 To array.Count - 1
                Struct = array(i)
                With Struct
                    .Age += 1
                End With
                array(i) = Struct
            Next

            'Approach using ENUMERATOR
            For Each Struct In array
                With Struct
                    .Age += 1
                End With
            Next

        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Sub

The line

                array(i) = Struct

which is appropriate for the first, modifies the collection, thus producing an error in the second.  You should use either one or the other.

Roger
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

724 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