|
|
|
start date: Fri, 17 Aug 2007 16:12:04 -0400,
posted on: microsoft.public.dotnet.framework
back
| Thread Index |
|
1
Mario Vargas
|
|
2
Scott M. am
|
Overriding Equals
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
Date:Fri, 17 Aug 2007 16:12:04 -0400
Author:
|
Re: Overriding Equals
I would consider creating a custom collection class and overriding the
Equals() method on that. Then use that class from within your new class.
"Mario Vargas" wrote in message
news:%23B48jrQ4HHA.5216@TK2MSFTNGP05.phx.gbl...
> 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
>
>
Date:Fri, 17 Aug 2007 16:49:31 -0400
Author:
|
|
|