?
Solved

Collection was modified; enumeration operation may not execute

Posted on 2011-09-02
2
Medium Priority
?
273 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
[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
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 2000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

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…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

801 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