Collection was modified; enumeration operation may not execute.

Zingoolation

New member
Joined
Jun 26, 2005
Messages
1
Programming Experience
5-10
This function is supposed to delete duplications from an ArrayList,

Public Function RemoveDuplicationIntegers(ByRef TheArray As ArrayList) As ArrayList

Dim TMP1 As ArrayList

TMP1 = TheArray

Dim tmpValue1, tmpValue2 As Integer

Dim tmpfirstvalue As String = TMP1.Item(0)

Dim TMP2 As New ArrayList



TMP2.Add(tmpfirstvalue)

Dim Found As Boolean = False

For Each tmpValue1 In TMP1

Found =
False

For Each tmpValue2 In TMP2

If tmpValue2 = tmpValue1 Then

Found = True

Exit For

End If

If Found = False Then

TMP2.Add(tmpValue1)

End If

Next

Next

Return (TMP2)

End Function

But it gives me this error:

Collection was modified; enumeration operation may not execute.


Does anyone know what's wrong with this code?


 
You can't use a For Each loop on a collection when you are deleting items because the enumeration of items is then changed. Use the following code to work from the end of the collection to the beginning:
VB.NET:
For i As Integer = myCollection.Count - 1 To 0 Step -1
    If <some condition> Then
        myCollection.RemoveAt(i)
    End If
Next
The alternative is to use a While loop with a separate index variable that you only increment if an item is not deleted:
VB.NET:
Dim index As Integer = 0

While index < myCollection.Count
    If <some condition> Then
        myCollection.RemoveAt(index)
    Else
        index += 1
    End If
End While
 
jm! though your answer is true but where he is trying to remove the item from collection.so if he is not then what u trying to suggest for (sorry if i couldt able to pick)
 
That is a very good point. You seem to be assuming that I'm actually smart enough to read the questions I'm answering :) I assumed that items wer being removed from the name of the function.

Zingoolation, I've had another, proper look at your code and you are doing some odd things. I'd be interested to see exactly how you are calling this function. You are passing the argument by reference, then you assign it to local variable. No items are altered in that variable but you return another ArrayList altogether. You normally pass a variable by reference if you intend to change that reference. Even if you change the items of the ArrayList you pass as an argument you still don't need to pass it by reference because you are not changing the refence itself. Here is a nice, simple procedure that will remove the duplicates from an ArrayList of integers:
VB.NET:
	[color=Blue]Public Sub[/color] RemoveDuplicationIntegers([color=Blue]ByVal[/color] TheArray [color=Blue]As[/color] ArrayList)
		[color=Blue]For[/color] i [color=Blue]As Integer[/color] = TheArray.Count - 1 [color=Blue]To[/color] 0 [color=Blue]Step[/color] -1
			[color=Blue]If[/color] TheArray.IndexOf(TheArray(i)) <> i [color=Blue]Then[/color]
			    [color=Green]'This is not the first occurrence of this value so remove it.[/color]
				TheArray.RemoveAt(i)
			[color=Blue]End If[/color]
		[color=Blue]Next[/color]
	[color=Blue]End Sub[/color]
Note that this is a procedure, not a function. It operates directly on the ArrayList you pass to it and does not return anything. Also, the ArrayList is passed by value, rather than by reference, because you are manipulating the object referred to, not the reference itself.

If you aren't sure how this works, IndexOf returns the index of first occurrence of a particular value in the collection. Say you are at index 10, and the value at index 10 is 2000. If IndexOf(2000) returns an index other than 10 then that means that index 10 is not the first occurrence of 2000, so it is a duplicate that can be removed. All but the first occurrence of each value will be removed in this way. You start at the end of the collection for the reason I mentioned in my previous post.
 
yeah now u seem to look his code , but what i even not understand what he is trying to do with passing by reference and then returning the other ArrayList . However i have suggested to him the solution from what he is trying to do without regard weather it is right or wrong.
actually now i tell u what was the reason of his error, his arrayList expanded between the iteration which will creates the same problemas during for each it is is prohibited (as what i think not tested yet). so it causes him problems
 
Given that the error message says that the collection was modified, the only options are that either the ArrayList had an item or items removed or it had an item or items added.

Zingoolation, I think it is safe to say that you don't quite understand what a reference type is. Any object that is an instance of a "class" is a reference type object. When you say something like var1 = var2 with reference types, what you are actually saying is "var1 now refers to the same object that var2 refers to". By the end of your function, there is only one actual ArrayList object and there are three variables that refer to it (TheArray, TMP1 and TMP2). If you add or remove an item using any of these references, the change will also be reflected in the others. You should really do some reading about value types and reference types or you are bound to make many more mistakes like this.
 
Zingoolation, I hope you didn't take my previous post too harshly. I wasn't trying to imply anything about you except that you have fallen into a trap that almost everyone new to object-oriented programming does. The concept of references and instances takes some getting used to. It can even be more difficult for programmers coming from a non-OO language than for completely new developers. Just be glad that you are only dealing with references and not actual pointers, like in C++. I would recommend reading about value types and reference types to any developer. For instance, I'd guess that there are a number of developers who think they know all about value and reference types who couldn't tell you why you can't change the properties of a structure returned by a read-only property but you can change the properties of a class under the same circumstances.
 
Back
Top