ForEach & Remove Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert W.

    ForEach & Remove Problem

    Here's some pseudo-code that describes what I'm trying to do:

    foreach(object in collection)
    {
    if (certain-condition)
    collection.Remo ve(object);
    }

    The problem with this is that foreach gets messed up if anything is deleted
    from the collection. What's the best way to handle such a situation?

    --
    Robert W.
    Vancouver, BC


  • Michael D. Ober

    #2
    Re: ForEach & Remove Problem

    In VB 2005, use

    for i as Integer in collection.coun t -1 to 0 step -1
    if collection(i) = condition then collection.remo veat(i)
    next i

    Iterators don't like removing objects.

    Mike Ober.

    "Robert W." <RobertW@discus sions.microsoft .comwrote in message
    news:963230B3-3CEA-448C-85E8-874A6092EB0C@mi crosoft.com...
    Here's some pseudo-code that describes what I'm trying to do:
    >
    foreach(object in collection)
    {
    if (certain-condition)
    collection.Remo ve(object);
    }
    >
    The problem with this is that foreach gets messed up if anything is
    deleted
    from the collection. What's the best way to handle such a situation?
    >
    --
    Robert W.
    Vancouver, BC

    >
    >


    Comment

    • Robert W.

      #3
      Re: ForEach &amp; Remove Problem

      And in C# 2003 ?

      --
      Robert W.
      Vancouver, BC




      "Michael D. Ober" wrote:
      In VB 2005, use
      >
      for i as Integer in collection.coun t -1 to 0 step -1
      if collection(i) = condition then collection.remo veat(i)
      next i
      >
      Iterators don't like removing objects.
      >
      Mike Ober.
      >
      "Robert W." <RobertW@discus sions.microsoft .comwrote in message
      news:963230B3-3CEA-448C-85E8-874A6092EB0C@mi crosoft.com...
      Here's some pseudo-code that describes what I'm trying to do:

      foreach(object in collection)
      {
      if (certain-condition)
      collection.Remo ve(object);
      }

      The problem with this is that foreach gets messed up if anything is
      deleted
      from the collection. What's the best way to handle such a situation?

      --
      Robert W.
      Vancouver, BC

      >
      >
      >
      >

      Comment

      • Mark Wilden

        #4
        Re: ForEach &amp; Remove Problem

        "Robert W." <RobertW@discus sions.microsoft .comwrote in message
        news:596E5BD3-D6E0-41DF-8CB5-260287010025@mi crosoft.com...
        >for i as Integer in collection.coun t -1 to 0 step -1
        > if collection(i) = condition then collection.remo veat(i)
        >next i
        And in C# 2003 ?
        Oh, c'mon. At least make an -effort-.

        ///ark


        Comment

        • Nicholas Paldino [.NET/C# MVP]

          #5
          Re: ForEach &amp; Remove Problem

          Robert,

          It doesn't matter, this has been a limitation in C# from version 1.0
          (for good reason).


          --
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard. caspershouse.co m

          "Robert W." <RobertW@discus sions.microsoft .comwrote in message
          news:596E5BD3-D6E0-41DF-8CB5-260287010025@mi crosoft.com...
          And in C# 2003 ?
          >
          --
          Robert W.
          Vancouver, BC

          >
          >
          >
          "Michael D. Ober" wrote:
          >
          >In VB 2005, use
          >>
          >for i as Integer in collection.coun t -1 to 0 step -1
          > if collection(i) = condition then collection.remo veat(i)
          >next i
          >>
          >Iterators don't like removing objects.
          >>
          >Mike Ober.
          >>
          >"Robert W." <RobertW@discus sions.microsoft .comwrote in message
          >news:963230B 3-3CEA-448C-85E8-874A6092EB0C@mi crosoft.com...
          Here's some pseudo-code that describes what I'm trying to do:
          >
          foreach(object in collection)
          {
          if (certain-condition)
          collection.Remo ve(object);
          }
          >
          The problem with this is that foreach gets messed up if anything is
          >deleted
          from the collection. What's the best way to handle such a situation?
          >
          --
          Robert W.
          Vancouver, BC

          >
          >
          >>
          >>
          >>
          >>

          Comment

          • Robert W.

            #6
            Re: ForEach &amp; Remove Problem

            I don't know if this is the optimum solution, but here's what I've come up:

            int i = 0;
            do
            {
            ObjectClass object = collection[i];
            if (test == false)
            i++;
            else
            collection.Remo ve(object);
            } while (i < collection.Coun t);




            --
            Robert W.
            Vancouver, BC




            "Nicholas Paldino [.NET/C# MVP]" wrote:
            Robert,
            >
            It doesn't matter, this has been a limitation in C# from version 1.0
            (for good reason).
            >
            >
            --
            - Nicholas Paldino [.NET/C# MVP]
            - mvp@spam.guard. caspershouse.co m
            >
            "Robert W." <RobertW@discus sions.microsoft .comwrote in message
            news:596E5BD3-D6E0-41DF-8CB5-260287010025@mi crosoft.com...
            And in C# 2003 ?

            --
            Robert W.
            Vancouver, BC




            "Michael D. Ober" wrote:
            In VB 2005, use
            >
            for i as Integer in collection.coun t -1 to 0 step -1
            if collection(i) = condition then collection.remo veat(i)
            next i
            >
            Iterators don't like removing objects.
            >
            Mike Ober.
            >
            "Robert W." <RobertW@discus sions.microsoft .comwrote in message
            news:963230B3-3CEA-448C-85E8-874A6092EB0C@mi crosoft.com...
            Here's some pseudo-code that describes what I'm trying to do:

            foreach(object in collection)
            {
            if (certain-condition)
            collection.Remo ve(object);
            }

            The problem with this is that foreach gets messed up if anything is
            deleted
            from the collection. What's the best way to handle such a situation?

            --
            Robert W.
            Vancouver, BC



            >
            >
            >
            >
            >
            >
            >

            Comment

            • John B

              #7
              Re: ForEach &amp; Remove Problem

              Robert W. wrote:
              I don't know if this is the optimum solution, but here's what I've come up:
              >
              int i = 0;
              do
              {
              ObjectClass object = collection[i];
              if (test == false)
              i++;
              else
              collection.Remo ve(object);
              } while (i < collection.Coun t);
              >
              >
              >
              >
              Try a reverse for loop;

              for(int i = col.Count -1; i >= 0; i--)
              {
              if(..test..)
              {
              col.Remove(i);
              }
              }

              Comment

              • Christian Schönig

                #8
                Re: ForEach &amp; Remove Problem

                Hi Robert,

                I solved it like this:

                SortedList temp=new SortedList(clie ntsUse);
                foreach (object key in temp.Keys)
                {
                if (key.Equals(lbC lients.Selected Item)) clientsUse.Remo ve(key);
                }

                So make a copy and remove the stuff from the original list You have copied from!

                Problem solved?

                Chris


                "Robert W." <RobertW@discus sions.microsoft .comschrieb im Newsbeitrag news:963230B3-3CEA-448C-85E8-874A6092EB0C@mi crosoft.com...
                Here's some pseudo-code that describes what I'm trying to do:

                foreach(object in collection)
                {
                if (certain-condition)
                collection.Remo ve(object);
                }

                The problem with this is that foreach gets messed up if anything is deleted
                from the collection. What's the best way to handle such a situation?

                --
                Robert W.
                Vancouver, BC

                >

                Comment

                • david ullua

                  #9
                  Re: ForEach &amp; Remove Problem

                  You can use for statement both in VS 2005 or 2003:

                  for( int i=collection.Co unt;i>=0;i--)
                  {
                  //...
                  }

                  Robert W. wrote:
                  Here's some pseudo-code that describes what I'm trying to do:
                  >
                  foreach(object in collection)
                  {
                  if (certain-condition)
                  collection.Remo ve(object);
                  }
                  >
                  The problem with this is that foreach gets messed up if anything is deleted
                  from the collection. What's the best way to handle such a situation?
                  >
                  --
                  Robert W.
                  Vancouver, BC
                  www.mwtech.com

                  Comment

                  • Larry Lard

                    #10
                    Re: ForEach &amp; Remove Problem


                    Robert W. wrote:
                    Here's some pseudo-code that describes what I'm trying to do:
                    >
                    foreach(object in collection)
                    {
                    if (certain-condition)
                    collection.Remo ve(object);
                    }
                    >
                    The problem with this is that foreach gets messed up if anything is deleted
                    from the collection. What's the best way to handle such a situation?
                    The solutions offered you that rely on iterating by an index might well
                    work, but make me think 'yuck' when I read them. I would prefer:

                    // terrible pseudocode follows
                    // keepthese will hold the objects we want to keep
                    collection keepthese = new collection
                    foreach object in originalcollect ion
                    // if not removing this object, then keep it
                    if (!removalcondit ion)
                    keepthese.add(o bject);

                    // now we have just those objects we want to keep
                    // so keep them
                    originalcollect ion = keepthese;


                    For anyone worried about the cost of this, remember there is no actual
                    copying of objects here, just copying of references, which is cheap.

                    --
                    Larry Lard
                    Replies to group please
                    When starting a new topic, please mention which version of VB/C# you
                    are using

                    Comment

                    • John Duval

                      #11
                      Re: ForEach &amp; Remove Problem

                      Hi Robert,
                      It might be simplest for you just to iterate backwards through the
                      collection, but Eric Gunnerson had a good article on an "IterIsolat e"
                      utility class that handles cases like this. You might want to check
                      out his MSDN article:



                      John

                      Comment

                      • Ignacio Machin \( .NET/ C# MVP \)

                        #12
                        Re: ForEach &amp; Remove Problem

                        Hi,

                        I also prefer a reverse loop, it does not affect the index.

                        Another possible solution that can be used in a collection that cannot be
                        used with index is using a temporary list to hold the values being deleted:

                        ArrayList ar = new ArrayList();

                        foreach(object o in collection)
                        {
                        if (certain-condition)
                        ar.Add(o);
                        }

                        foreach(object o in ar)
                        coollection.Rem ove( o);


                        --
                        --
                        Ignacio Machin,
                        ignacio.machin AT dot.state.fl.us
                        Florida Department Of Transportation
                        >>
                        Try a reverse for loop;
                        >
                        for(int i = col.Count -1; i >= 0; i--)
                        {
                        if(..test..)
                        {
                        col.Remove(i);
                        }
                        }

                        Comment

                        Working...