Re: Newbie - delete item from collection

Chris Wuestefeld <[email protected]>
Newsgroups gmane.comp.windows.devel.dotnet.advanced
Message-ID <00be01c948f5$da83fc00$8f8bf400$@com>
As you're scanning the list, you're pulling the carpet out from under
yourself.

Consider a list with three items, index 0, 1, and 2:
[0] = A, ErrorCode = 99
[1] = B, ErrorCode = 99
[2] = C, ErrorCode = 0

_someList[0].ErrorCode = 99, so we remove it. At this time, the thing that
had been at [1] shifts downward in index, leaving this:
[0] = B, ErrorCode = 99
[1] = C, ErrorCode = 0

No increment idx to 1, and look at _someList[1]. You'll see that this has a
low ErrorCode, so we can skip it.

So you deleting as you're doing, you're skipping any item immediately
following a removed item.

If you just change your loop to count downwards, from _someList.Count-1 to
0, everything will work out fine.

===================================
View archives and manage your subscription(s) at http://peach.ease.lsoft.com/archives
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.