Re: How do you modify an array while iterating through it?
From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 02/17/04
- Next message: Jay B. Harlow [MVP - Outlook]: "Re: Casting - Cant convert VB sample."
- Previous message: Marco Liedekerken: "Re: C# exceptions not showing filename/line numbers (PDB issue?)"
- In reply to: Gustaf Liljegren: "Re: How do you modify an array while iterating through it?"
- Next in thread: Gustaf Liljegren: "Re: How do you modify an array while iterating through it?"
- Reply: Gustaf Liljegren: "Re: How do you modify an array while iterating through it?"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 17 Feb 2004 13:37:46 -0000
Gustaf Liljegren <gustaf.liljegren@bredband.net> wrote:
<snip>
> So, the problem remaining is how to add another Account within that loop.
> The logic is:
>
> 1. See if the account already exists in the array.
> 2. If it does, add a new amount to the old one. If not, create a new account
> in the array.
>
> I'll see if I can come up with a simple example to work on.
You can't change a list while you're iterating through it. One common
solution is to build up another list containing the items you *want* to
add to the list, and then add all of them after you've finished
iterating. (You could use ArrayList.AddRange for that.)
-- Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
- Next message: Jay B. Harlow [MVP - Outlook]: "Re: Casting - Cant convert VB sample."
- Previous message: Marco Liedekerken: "Re: C# exceptions not showing filename/line numbers (PDB issue?)"
- In reply to: Gustaf Liljegren: "Re: How do you modify an array while iterating through it?"
- Next in thread: Gustaf Liljegren: "Re: How do you modify an array while iterating through it?"
- Reply: Gustaf Liljegren: "Re: How do you modify an array while iterating through it?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|