Solved

ArrayList.Contains() -- using objects of different types

Posted on 2004-08-21
11
2,594 Views
Last Modified: 2007-12-19
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(suppliedObj) it would use Object.Equals(suppliedObj, ContainedObj)

Thanks for your thoughts,
Sean.
0
Comment
Question by:SeanStapleton
11 Comments
 
LVL 22

Expert Comment

by:mnasman
ID: 11862655
From MSDN help about Contains method:

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.
0
 
LVL 1

Author Comment

by:SeanStapleton
ID: 11862705
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.
0
 
LVL 1

Author Comment

by:SeanStapleton
ID: 11862775
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)


0
 
LVL 20

Expert Comment

by:ihenry
ID: 11862883
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.
0
 
LVL 20

Expert Comment

by:ihenry
ID: 11862892
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.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 1

Author Comment

by:SeanStapleton
ID: 11862934
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.
0
 
LVL 20

Accepted Solution

by:
ihenry earned 50 total points
ID: 11863020

I've tried a quick debugging and I think my assumption is correct. I'm quite sure the implementation of ArrayList.Contains would look something similar like this,

public virtual bool Contains(Object item)
{
      if (item==null)
      {
            ....
            ....
      }
      else
      {
            foreach ( object o in items )
                  if (item.Equals(o) )
                        return true;
                  return false;
            }
      }
}

So when an integer parameter is passed, it evaluates int.Equals() instead
0
 
LVL 20

Expert Comment

by:ihenry
ID: 11863033
I to put a breakpoint in the Permission.Equal method

plist.Contains(p); execution stops at the breakpoint
plist.Contains(pId); nothing happen
0
 
LVL 1

Author Comment

by:SeanStapleton
ID: 11863111
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.
0
 
LVL 20

Expert Comment

by:ihenry
ID: 11863144

yes, I was wrong.. thanks for the correction and also for the points.
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 11864931
Why don't you just override ArrayList.Contains, or cast your integers to Permission objects when you call ArrayList.Contains?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now