Solved

Collection was modified; enumeration operation may not execute

Posted on 2011-09-02
2
253 Views
Last Modified: 2012-05-12
Collection was modified; enumeration operation may not execute is the error i am getting when trying to remove a value from the dictionary.

This is been done within a for loop

 For Each pair In storeddate
                '#################################################
                '# check How many days holiday the user has left #
                '#################################################
                If pair.Value = "full" Then
                    daysleft = daysleft + 1
                Else
                    daysleft = daysleft + 0.5
                End If

                Label3.Text = daysleft


                ' if days left = more then what you can have off then disable button
                Dim holidayleft As Decimal = dateallreadybooked.countholidaysleft("Jarratt")

                If daysleft > holidayleft Then
                    Label1.Text = "you have no holidays left to book."
                    ' remove from the dictonary
                     storeddate.Remove(e.Day.Date)
                Else

                    If e.Day.Date = pair.Key Then
                        Label2.Text &= pair.Key & ", " & pair.Value & "<br />"




                        Dim howlong As String = pair.Value
                        Select Case howlong
                            Case "Am"

                                am.ImageUrl = "images/Blue_Button.jpg"


                            Case "Pm"

                                pm.ImageUrl = "images/Blue_Button.jpg"


                            Case "full"

                                full.ImageUrl = "images/Blue_Buttonlarge.jpg"

                        End Select

                        Session("storeddate") = storeddate 'save the dictonary 
                    End If
                End If
            Next

Open in new window

the error has only started since adding  storeddate.Remove(e.Day.Date)
0
Comment
Question by:runnerjp2005
2 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36473865
Line 20 is the error. You cannot delete from a collection for which you are iterating over using a For Each. If you want to remove from a collection within a loop, use a backwards-iterating For loop.
0
 
LVL 15

Accepted Solution

by:
x77 earned 500 total points
ID: 36474296
On sample code, I remove values from a dictionary using enumerator.

Note that when I remove a entry, I restart the enumerator (Continue do -> Dim ekv = dSubTr.Getenumerator ...)

You can´nt do it using for each ...
Do While dSubTr.Count > 0 'Tareas Resumen Pendientes de calcular
            Dim eKv = dSubTr.GetEnumerator, kv As KeyValuePair(Of String, List(Of String))
            While eKv.MoveNext
               kv = eKv.Current : i = kv.Value.Count - 1
               For n = 0 To i
                   If dSubTr.ContainsKey(kv.Value(n)) Then Continue While 'Depende de otra Tarea Resumen
               Next
               a = Item(kv.Key)
               For n = 0 To i
                   a.ajTResumen(Item(kv.Value(n)))
               Next
               'Eliminar de pendientes
               dSubTr.Remove(a.Id) : Continue Do
            End While

            LogSb.AppendLine.AppendLine("Tareas Resumen con bucle en definición")

Open in new window

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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…

860 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