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. Getting InvalidOperationException when I modify an arraylist

Getting InvalidOperationException when I modify an arraylist

Scheduled Pinned Locked Moved C#
questiondiscussion
3 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.
  • H Offline
    H Offline
    humblepgmr
    wrote on last edited by
    #1

    foreach (myobject value in copy){ ... copy.RemoveAt(copy.IndexOf(value)); ... } The first iteration works okay, but when the loop starts again, it throws and InvalidOperationException. Any thoughts as to how I can remove an element within the arraylist whilst still using the same list?

    L M 2 Replies Last reply
    0
    • H humblepgmr

      foreach (myobject value in copy){ ... copy.RemoveAt(copy.IndexOf(value)); ... } The first iteration works okay, but when the loop starts again, it throws and InvalidOperationException. Any thoughts as to how I can remove an element within the arraylist whilst still using the same list?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, I see two ways to get rid of it: 1. use a second ArrayList: 1a. if only a few to remove, store the indices to be removed in another empty ArrayList, then in a second loop, remove those (warning: indices change while removing, so best do the second loop backwards) 1b. if most of them to be removed, start a new ArrayList and copy the items you want to keep, then throw away the original ArrayList OR 2. don't use foreach, instead use a for loop that runs backwards (or compensate for the indices changing when removing). :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


      1 Reply Last reply
      0
      • H humblepgmr

        foreach (myobject value in copy){ ... copy.RemoveAt(copy.IndexOf(value)); ... } The first iteration works okay, but when the loop starts again, it throws and InvalidOperationException. Any thoughts as to how I can remove an element within the arraylist whilst still using the same list?

        M Offline
        M Offline
        Matthew Cuba
        wrote on last edited by
        #3

        humblepgmr wrote:

        foreach (myobject value in copy){ ... copy.RemoveAt(copy.IndexOf(value)); ... }

        Hi there, There was some information at Microsoft about this. You might consider checking out http://support.microsoft.com/kb/555972 to read about it. Looks like a workaround involves creating a copy of the collection within the foreach loop. Good Luck!

        You can't teach people to be lazy - either they have it, or they don't. -Dagwood Bumbstead

        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