Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Remove items from ArrayList

Reply
 
Old 03-27-2009   #1 (permalink)
Curious


 
 

Remove items from ArrayList

Hi,

I have an array list in which some items are marked as "ShouldRemove".
When I loop through each item, I should remove these items. However, I
believe that I will get "Collection was modified; enumeration
operation may not execute" error.

foreach (BenchmarkPrice bp in mBenchmarkPriceList)
{
if (bp.ShouldRemove == true)
{
// This won't work because this alters the length
of the array list, mBenchmarkPriceList
mBenchmarkPriceList.Remove (bp);
}
}

Any advice on how to get this done safely?

My System SpecsSystem Spec
Old 03-27-2009   #2 (permalink)
Jack Jackson


 
 

Re: Remove items from ArrayList

On Fri, 27 Mar 2009 06:59:35 -0700 (PDT), Curious
<fir5tsight@xxxxxx> wrote:
Quote:

>Hi,
>
>I have an array list in which some items are marked as "ShouldRemove".
>When I loop through each item, I should remove these items. However, I
>believe that I will get "Collection was modified; enumeration
>operation may not execute" error.
>
> foreach (BenchmarkPrice bp in mBenchmarkPriceList)
> {
> if (bp.ShouldRemove == true)
> {
> // This won't work because this alters the length
>of the array list, mBenchmarkPriceList
> mBenchmarkPriceList.Remove (bp);
> }
> }
>
>Any advice on how to get this done safely?
Don't use an enumeration. Instead of For Each, use a loop from the
end to the beginning:

For indx As Integer = mBenchmarkPriceList.Count - 1 TO 1 STEP -1
Dim bp As BenchMarkPrice = mBenchmarkPriceList(indx)
...
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
How to remove recognize/remove items under "Services" tag in SYSTEM CONFIG window? General Discussion
Loop only the last 10 items in arraylist .NET General
How to remove function: items at startup PowerShell
Thread-safety: Change property of items in arraylist versus removingitems from the arraylist .NET General
remove items from index manually Vista file management


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46