[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

Trying to remove item from array list kicks me out of sub

When i run the attached for loop it does fine until it gets to an item containing the word locker.  Then it kicks out of the sub without doing anything else.  Not just the for loop but the entire load sub.
For Each item As String In packlist
            If item.Contains("LOCKER") Then
                packlist.Remove(item)
            End If
        Next

Open in new window

0
Millkind
Asked:
Millkind
1 Solution
 
CodeCruiserCommented:
You need to use a For loop instead of For Each and run in reverse

For i As Integer = packlist.Count - 1 to 0 step -1
            If packList(i).Contains("LOCKER") Then
                packlist.RemoveAt(i)
            End If
Next
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Exactly.  The reason it fails is because you can't modify the "packlist" collection while iterating over it with a "For Each".
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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now