[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Collection was modified; enumeration operation may not execute

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

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 …
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

650 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