Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Looking for a "best practice" advice

Looking for a "best practice" advice

Scheduled Pinned Locked Moved C#
questiondiscussion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jan R Hansen
    wrote on last edited by
    #1

    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

    D R 2 Replies Last reply
    0
    • J Jan R Hansen

      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

      D Offline
      D Offline
      dmivlad
      wrote on last edited by
      #2

      Maybe something like that: while(col.count > 0) { processItem(col[0]); col.RemoveAt(0); } But I am not sure it is a better way ;)

      1 Reply Last reply
      0
      • J Jan R Hansen

        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

        R Offline
        R Offline
        Ravi Bhavnani
        wrote on last edited by
        #3

        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

        J 1 Reply Last reply
        0
        • R Ravi Bhavnani

          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

          J Offline
          J Offline
          Jan R Hansen
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups