Looking for a "best practice" advice
-
Hi, I often end up with a collection of items I have to process. Say - an arraylist of "commands". I want to process these command and then delete the command from the collection. As you cannot modify a collection during a foreach, I often process it in reverse and delete the last (just processed) item like this lock (col) { for (int i = col.count-1 ; i <=0 ; i--) { processItem(col[i]); col.RemoveAt(i); } } Is there a better way to do this ? /Jan Do you know why it's important to make fast decisions? Because you give yourself more time to correct your mistakes, when you find out that you made the wrong one. _Chris Meech on deciding whether to go to his daughters graduation or a Neil Young concert_p
-
Hi, I often end up with a collection of items I have to process. Say - an arraylist of "commands". I want to process these command and then delete the command from the collection. As you cannot modify a collection during a foreach, I often process it in reverse and delete the last (just processed) item like this lock (col) { for (int i = col.count-1 ; i <=0 ; i--) { processItem(col[i]); col.RemoveAt(i); } } Is there a better way to do this ? /Jan Do you know why it's important to make fast decisions? Because you give yourself more time to correct your mistakes, when you find out that you made the wrong one. _Chris Meech on deciding whether to go to his daughters graduation or a Neil Young concert_p
-
Hi, I often end up with a collection of items I have to process. Say - an arraylist of "commands". I want to process these command and then delete the command from the collection. As you cannot modify a collection during a foreach, I often process it in reverse and delete the last (just processed) item like this lock (col) { for (int i = col.count-1 ; i <=0 ; i--) { processItem(col[i]); col.RemoveAt(i); } } Is there a better way to do this ? /Jan Do you know why it's important to make fast decisions? Because you give yourself more time to correct your mistakes, when you find out that you made the wrong one. _Chris Meech on deciding whether to go to his daughters graduation or a Neil Young concert_p
Jan R Hansen wrote:
want to process these command and then delete the command from the collection.
Since
processItem()
doesn't know anything about the commands in the collection (other than the one it's processing), you might consider simply doing this:lock (col) {
foreach (Command c in col)
processItem (c);
col.Clear();
}If
Command
is a heavyweight object, you could try this approach:lock (col) {
for (int i=0; (i < col.Count); i++) {
Command c = col[i] as Command;
processItem (c);
col[i] = null;
}
col.Clear();
}/ravi My new year's resolution: 2048 x 1536 Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com
-
Jan R Hansen wrote:
want to process these command and then delete the command from the collection.
Since
processItem()
doesn't know anything about the commands in the collection (other than the one it's processing), you might consider simply doing this:lock (col) {
foreach (Command c in col)
processItem (c);
col.Clear();
}If
Command
is a heavyweight object, you could try this approach:lock (col) {
for (int i=0; (i < col.Count); i++) {
Command c = col[i] as Command;
processItem (c);
col[i] = null;
}
col.Clear();
}/ravi My new year's resolution: 2048 x 1536 Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com
Thanks - I overlooked that I had to lock the collection anyway, so I could just as well use the topmost example. Thanks again ! :-D Do you know why it's important to make fast decisions? Because you give yourself more time to correct your mistakes, when you find out that you made the wrong one. Chris Meech on deciding whether to go to his daughters graduation or a Neil Young concert