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