Question Problem with for each loop

indz1989

New member
Joined
Nov 26, 2012
Messages
2
Programming Experience
Beginner
I just started using VB.net for about an hour now and i encountered a problem with regards to using for each loop. Can anyone tell me why i got an error message saying that

"List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change."

Here is my code

VB.NET:
If (ListBox1.Items.Count <> 0) Then
            For Each fern As String In ListBox1.Items
                If (fern = TextBox1.Text) Then
                    MessageBox.Show("The value the you are going to add  is already in the list", "Invalid Value", MessageBoxButtons.OK,  MessageBoxIcon.Information)
                Else
                    ListBox1.Items.Add(TextBox1.Text)
                End If
            Next 'This is where the error shows up
        Else
            ListBox1.Items.Add(TextBox1.Text)
        End If

Can anyone tell me why i got the error message? thanks
 
You don't need to for each it..

      If (ListBox1.Items.Count <> 0) Then

         If ListBox1.Items.Contains(TextBox1.Text) Then
            MessageBox.Show("The value the you are going to add  is already in the list", "Invalid Value", MessageBoxButtons.OK, MessageBoxIcon.Information)
         Else
            ListBox1.Items.Add(TextBox1.Text)
         End If

      Else

         ListBox1.Items.Add(TextBox1.Text)

      End If



The reason you got the error message is exactly as the message stated:

"List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change."

Because your count is bound to the number of items in the list box (For Each.... in ListBox1.Items), when this number changes dynamically (i.e new items are added), this can cause an undesirable/unpredictable effect. Also, the way you were using For Each, if vb had allowed it, would have seen you adding one entry for every existing entry in the listbox.. i.e if there were 10 items in the list box, and you wanted to add an 11th item, for each would have checked if item 1 was not the new item, and then added it, then would have checked if item 2 was not the new item, and added it again until it eventually got to the 11th item and saw it was already there, by which time you had added your new item 10 times.
 
When you use a For Each loop to enumerate a list, you cannot change the list inside the loop. You are looping through the Items collection of the ListBox and you Add an item to that collection inside that loop, which is not allowed.

Besides that, you code doesn't really make sense. You are testing each item in the ListBox and, for each one, if that item is not the same as what's in a TextBox, you add the contents of the TextBox to the ListBox. That means that if you have X items in the ListBox and none of them match what's in the TextBox, you would be adding the contents of the TextBox X times. You wouldn't add the contents of the TextBox until the loop had finished. You would start with a flag set False and then loop through all the items and set the flag True if you found a match. At the end of the loop you test the flag and then either add the value or display the message.
 
Back
Top