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. Strange behavior with ArrayList remove [modified]

Strange behavior with ArrayList remove [modified]

Scheduled Pinned Locked Moved C#
databasehelp
3 Posts 2 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.
  • M Offline
    M Offline
    malharone
    wrote on last edited by
    #1

    I'm working with the following Remove code. My code is failing at line # 6 and throwing an exception. I don't understand why "index == -1" (see immidiate-window output) is evaluating to true, eventhough my immidiate window is returning true for existance of 'column' object in the ArrayList. Furthermore, if I comment out lines #4-7 then the all is golden. Any help would be appreciated. Thanks, - Malhar

    public class ColumnCollection
    {
    ArrayList _columnList; //Assume this is initialized and properly populated

    ...
    

    1: public void Remove (TreeListColumn column)
    2: {
    3: int index = _columnList.IndexOf (column)
    4: if (index == -1)
    5: {
    6: throw new ArgOutOfRangeException (...);
    7: }
    8: _columnList.RemoveAt (index);
    9: }

    ...
    

    }

    Immidiate/Command Window (Break point at line # 3): column == _columnList[4] ==> true Object.ReferenceEquals (column, _columnList[4] ==> true

    _columnList.IndexOf(column) ==> -1 :wtf:

    G 1 Reply Last reply
    0
    • M malharone

      I'm working with the following Remove code. My code is failing at line # 6 and throwing an exception. I don't understand why "index == -1" (see immidiate-window output) is evaluating to true, eventhough my immidiate window is returning true for existance of 'column' object in the ArrayList. Furthermore, if I comment out lines #4-7 then the all is golden. Any help would be appreciated. Thanks, - Malhar

      public class ColumnCollection
      {
      ArrayList _columnList; //Assume this is initialized and properly populated

      ...
      

      1: public void Remove (TreeListColumn column)
      2: {
      3: int index = _columnList.IndexOf (column)
      4: if (index == -1)
      5: {
      6: throw new ArgOutOfRangeException (...);
      7: }
      8: _columnList.RemoveAt (index);
      9: }

      ...
      

      }

      Immidiate/Command Window (Break point at line # 3): column == _columnList[4] ==> true Object.ReferenceEquals (column, _columnList[4] ==> true

      _columnList.IndexOf(column) ==> -1 :wtf:

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      It might have something to do with how the items are compared. The IndexOf method doesn't use the == operator or the ReferenceEquals method to compare the items, but the Equals method.

      --- b { font-weight: normal; }

      M 1 Reply Last reply
      0
      • G Guffa

        It might have something to do with how the items are compared. The IndexOf method doesn't use the == operator or the ReferenceEquals method to compare the items, but the Equals method.

        --- b { font-weight: normal; }

        M Offline
        M Offline
        malharone
        wrote on last edited by
        #3

        You are right, but the "Equals" method is not overridden and the TreeListColumn is using the base (object.Equals) method. (Not that it matters) But the value returned GetHashCode() is also the same for both the objects. - Malhar

        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