Original Post
In my application, I have a collection of objects that each have their own unique string and integer associated with them. I want to be able to identify the object either by its integer value or string value, so in my object class I have the following
Equals override:
public override bool Equals(object obj)
{
// Determine type
if(obj is string)
{
return (this.name == (string)obj);
}
else if(obj is int)
{
return (this.code == (int)obj);
}
else
{
return false;
}
}
I then have an ArrayList that contains a bunch of these objects. However, whenever I called the IndexOf method on my ArrayList, I find that it never invokes my overriden Equals, and thus always returns false. However, when I call Contains, it does use my overriden version.
To be more specific, I''m calling IndexOf through the indexer of a wrapper collection class, like so:
public CustomObject this [object index]
{
get
{
if(myObjects.IndexOf(index) == -1)
{
return null;
}
else
{
return (CustomObject)myObjects[ contentCodes.IndexOf(index) ];
}
}
}
This indexer always returns null, even if the item I''m looking for is indeed in the ArrayList (I''ve put a watch on the ArrayList, and it does contain what I''m looking for).
The indexer is called like this:
ObjectCollection["uniquestring"]; // Or ObjectCollection[uniqueinteger];It was my thinking that the object would eventually propogate down to Equals, and there the type would be resolved and the correct result returns, but I breakpoint Equals and it''s never called. Should I resort to an old-fashioned search loop, or is there something I''m doing wrong?