Overriding Equals

M

Mario Vargas

Hello all,

I am faced with a dilemma with a class I am working on. I am overriding the
Equals() method to be able to make comparisons with objects of the same
type, but this class has a member field that contains a collection. My
problem is, the two objects I am comparing are exactly the same but the
collections in each object are different. Is it a good idea to check
equality of member fields that are collections?

For example: class MyClass has two member fields: Name and Jobs. Jobs is a
JobCollection of Job objects. Both MyClass and JobCollection override
Equals(). I have a instance B of MyClass that contains 4 Jobs and in an
Array of MyClass objects, I have element C that contains the same name as B
but only consists of 3 Jobs. The Jobs are unsynchronized because when B
changed from 3 to 4, there was no way to notify the Array of MyClass objects
that B and C are the same and the new Job added to B had to be added to C as
well.

Thanks for your input! I know that for instance a collection such as
StringCollection doesn't override the Equals() method.

Mario
 
S

Scott M.

I would consider creating a custom collection class and overriding the
Equals() method on that. Then use that class from within your new class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top