You can't delete items in a list inside a For Each loop - it changes the list you are loop ingon, and that will always throw an exception, and very rightly.
Change your For Each to a For, and work backwards from the end to the beginning and you can delete them fine.
Alternatively, set up a new List and add each item you want to remove to that and then use a second For Each loop to call List.Remove for each of them.
" have no clue to get it right and I am seeking for help here.
"Change your For Each to a For, and work backwards from the end to the beginning and you can delete them fine.""
Well, you know how to change a For Each to a For:
Dim myList As New List(Of String)()
For Each s As String In myList
Console.WriteLine(s)
Next
Becomes:
Dim myList As New List(Of String)()
For i As Integer = 0 To myList.Length - 1
Dim s As String = myList(i)
Console.WriteLine(s)
Next
Yes?
So write the For so it runs from the highest index to the lowest:
Dim myList As New List(Of String)()
For i As Integer = myList.Length - 1 To 0 Step -1
Dim s As String = myList(i)
Console.WriteLine(s)
Next
That way, when you delete an item it doesn't alter the index of the next item you will look at:
Dim myList As New List(Of String)()
For i As Integer = myList.Length - 1 To 0 Step -1
Dim s As String = myList(i)
If s.StartsWith("Delete me") Then
myList.Remove(s)
End If
Next
Make sense?