Solved

Collection was modified; enumeration operation may not execute

Posted on 2011-09-02
2
247 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

914 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

13 Experts available now in Live!

Get 1:1 Help Now