0

I'm trying to get the current element that For Each is executing but I'm getting

InvalidOperationException: The list with which the enumerator is associated has been modified. The enumerator can only be used if the list remains constant.

That's the code:

For Each i As String In ListBox1.Items
    Try
        [..code]
    Catch ex As WebException
        If ex.Status = WebExceptionStatus.ProtocolError Then
            Dim Risposta As DialogResult
            Risposta = MessageBox.Show(String.Format("Errore: Impossibile trovare l'SWF {0}. Continuare?", i), "Errore durante il check degli SWF!", MessageBoxButtons.YesNo)
            If Risposta = System.Windows.Forms.DialogResult.Yes Then
                ListBox1.Items.Remove(i) <- Thats where the error pops up
            End If
            If Risposta = System.Windows.Forms.DialogResult.No Then
                Application.Exit()
            End If
        Else
            MessageBox.Show(String.Format("Errore: {0}", ex.Message), "Errore sconosciuto durante il check degli SWF!")
            Application.Exit()
        End If
    End Try

How can I fix this?

GSerg
  • 76,472
  • 17
  • 159
  • 346
Wez
  • 11
  • 2
  • Possible duplicate of [C# removing items from listbox](https://stackoverflow.com/questions/2096489/c-sharp-removing-items-from-listbox) – TnTinMn Aug 31 '18 at 19:02

2 Answers2

1

You cant modify an iterator, therefore you have to use a regular for loop.

For i as Integer = ListBox1.Items.Count() - 1 To 0 Step -1
    ListBox1.Items(i)          //to access
    ListBox1.Items.RemoveAt(i) //to remove
Next

added code fixes from comments

gia
  • 757
  • 5
  • 19
  • 3
    It's usual to iterate from the end to the start (`For i = ListBox1.Items.Count() - 1 To 0 Step -1`) so that removing an item doesn't change the indexes of the remaining items to be iterated over. – Andrew Morton Aug 31 '18 at 17:47
1

One possible solution is to store the items to be removed and then remove them after going through the ListBox items, like this:

Sub RemoveImpossible(itemsToRemove As List(Of String))
    For Each s In itemsToRemove
        ListBox1.Items.Remove(s)
    Next

End Sub

Sub X()
    Dim impossibleItems As New List(Of String)

    For Each i As String In ListBox1.Items
        Try
            ' [..code]
        Catch ex As WebException
            If ex.Status = WebExceptionStatus.ProtocolError Then
                Dim Risposta As DialogResult
                Risposta = MessageBox.Show(String.Format("Errore: Impossibile trovare l'SWF {0}. Continuare?", i), "Errore durante il check degli SWF!", MessageBoxButtons.YesNo)
                If Risposta = System.Windows.Forms.DialogResult.Yes Then
                    impossibleItems.Add(i)
                End If
                If Risposta = System.Windows.Forms.DialogResult.No Then
                    Application.Exit()
                End If
            Else
                MessageBox.Show(String.Format("Errore: {0}", ex.Message), "Errore sconosciuto durante il check degli SWF!")
                Application.Exit()
            End If
        End Try
    Next

    RemoveImpossible(impossibleItems)

End Sub

That way, some other processing on the items which did not work could be done afterwards, if that was a useful thing to do.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84