Solved

Collection was modified; enumeration operation may not execute

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

11 Experts available now in Live!

Get 1:1 Help Now