SeanStapleton
asked on
ArrayList.Contains() -- using objects of different types
I have an ArrayList of objects called Permission. Permission.Equals() has been overriden to allow comparison with int, and works as expected.
However, ArrayList.Contains() continues to return false. Your thoughts or advice are appreciated.
Permission p = new Permission(9);
int pId = 9;
ArrayList plist = new ArrayList();
plist.Add(p);
p.Equals(pId); // true
Object.Equals(p, pId); // true
pId.Equals(p); // false - as expected
Object.Equals(pId, p); // false - as expected
p.GetHashCode(); // 9
pId.GetHashCode(); // 9
plist.Contains(p); // true
plist.Contains(pId); // false
I thought .Contains() used the Equals method of the contained class? Otherwise I don't think i can make this work at all ... I'm clearly not going to rewrite the Equals() method of int?
It seems silly that ArrayList.Contains(supplie dObj) it would use Object.Equals(suppliedObj, ContainedObj)
Thanks for your thoughts,
Sean.
However, ArrayList.Contains() continues to return false. Your thoughts or advice are appreciated.
Permission p = new Permission(9);
int pId = 9;
ArrayList plist = new ArrayList();
plist.Add(p);
p.Equals(pId); // true
Object.Equals(p, pId); // true
pId.Equals(p); // false - as expected
Object.Equals(pId, p); // false - as expected
p.GetHashCode(); // 9
pId.GetHashCode(); // 9
plist.Contains(p); // true
plist.Contains(pId); // false
I thought .Contains() used the Equals method of the contained class? Otherwise I don't think i can make this work at all ... I'm clearly not going to rewrite the Equals() method of int?
It seems silly that ArrayList.Contains(supplie
Thanks for your thoughts,
Sean.
ASKER
Yes, I've read this. But Object.Equals takes two parameters, and MSDN doesn't specify which order the params are supplied in. Thought it appears I've discovered it.
ASKER
To clarify my previous comment
myObj.Equals(object rhs)
- 2 parameters: this & rhs (it's often called right hand side b/c there is a left)
Object.Equals(object item1, object item2)
- 2 parameters: item1 and item2 (can't access this from a static method, though i suppose could be accessing static vars of Object)
myObj.Equals(object rhs)
- 2 parameters: this & rhs (it's often called right hand side b/c there is a left)
Object.Equals(object item1, object item2)
- 2 parameters: item1 and item2 (can't access this from a static method, though i suppose could be accessing static vars of Object)
The documentation means
when this line is executed, plist.Contains(p);
p.Equals(pId) is evaluated, it returns true as expected
and when this line is executed, plist.Contains(pId);
pId.Equals(p) is evaluated, it returns false which is the correct behaviour.
when this line is executed, plist.Contains(p);
p.Equals(pId) is evaluated, it returns true as expected
and when this line is executed, plist.Contains(pId);
pId.Equals(p) is evaluated, it returns false which is the correct behaviour.
sorry wrong typo,
when this line is executed, plist.Contains(p);
Permission.Equals(p) is evaluated, it returns true as expected
and when this line is executed, plist.Contains(pId);
int.Equals(pId) is evaluated, it returns false which is the correct behaviour.
when this line is executed, plist.Contains(p);
Permission.Equals(p) is evaluated, it returns true as expected
and when this line is executed, plist.Contains(pId);
int.Equals(pId) is evaluated, it returns false which is the correct behaviour.
ASKER
pList is composed of Permission objects, so you are not quite right ihenry.
It appears that what happens is akin to...
bool Contains(obj rhs)
{
foreach(object obj in ArrayList.items)
{
if (rhs.Equals(obj)) return true;
}
}
what i had hoped for (and what makes more sense to me) was that it would instead call obj.Equals(rhs), so that by overloading the Equals of Permission I could handle int.
It appears that what happens is akin to...
bool Contains(obj rhs)
{
foreach(object obj in ArrayList.items)
{
if (rhs.Equals(obj)) return true;
}
}
what i had hoped for (and what makes more sense to me) was that it would instead call obj.Equals(rhs), so that by overloading the Equals of Permission I could handle int.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I to put a breakpoint in the Permission.Equal method
plist.Contains(p); execution stops at the breakpoint
plist.Contains(pId); nothing happen
plist.Contains(p); execution stops at the breakpoint
plist.Contains(pId); nothing happen
ASKER
ihenry, I agree with your implementation. It's what i discovered here, but just hoped it was otherwise...
My comment that you weren't quite on was a symantecs. Looking again, your two first comments are each half right...
when this line is executed, plist.Contains(p);
p.Equals(plist.item[1..n]) is evaluated, it returns true as expected
and when this line is executed, plist.Contains(pId);
pId.Equals(plist.item[1..n ]) is evaluated, it returns false which is the correct behaviour.
You get the points for confirmation and peace of mind. Thanks.
Sean.
My comment that you weren't quite on was a symantecs. Looking again, your two first comments are each half right...
when this line is executed, plist.Contains(p);
p.Equals(plist.item[1..n])
and when this line is executed, plist.Contains(pId);
pId.Equals(plist.item[1..n
You get the points for confirmation and peace of mind. Thanks.
Sean.
yes, I was wrong.. thanks for the correction and also for the points.
Why don't you just override ArrayList.Contains, or cast your integers to Permission objects when you call ArrayList.Contains?
This method performs a linear search; therefore, the average execution time is proportional to Count. That is, this method is an O(n) operation, where n is Count.
This method determines equality by calling Object.Equals.