Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Value Comparison 1

Status
Not open for further replies.

crazyboybert

Programmer
Jun 27, 2001
798
GB
Hi All

I want to compare two objects of the same type to test for value equality. I can't do this with Equals(object, object), object.Equals(object) or == as these all test for reference equality only.

I can see two options for performing this type of comparison. I can either override the Equals method for the object to specify a value comparison, or implement the IComparable interface and use the CompareTo method to provide a value comparison.

The thing is I have several classes I wish to be able to perform this type of comparison on and they all have multiple value properties to test. This adds up to a fair bit of work and I was hoping (probably fruitlessly I admit) that someone would have a quicker solution to anabling a value comparison.

Thanks

Rob

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
You have no choice.
Implement as a minimum the == and != operators:
public static MyClass operator ==(MyClass x, MyClass y)
{
// Put here the conditions when two objects are equals
if (x.value == y.value)
return true;
return false;
}

// Inequality operator.
public static MyClass operator !=(MyClass x, MyClass y)
{
//...
}
or use IComparable to determine the result of two object comparison.
For example , two string objects are equals if the soundex(s1) == soundex(s2).
-obislavu-
 
It is possible to use reflection to compare the contents of an object, but it will be incredibly slow. You'll be better off implementing those operators like obislavu says, that way you'll have control over what gets compared (there may be parts of your objects that you don't care about in a comparison)

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Thanks guys

It took me a couple of takes on obislavu's answer to realise what was going on with the code. I learn cool stuff about C# (like operator overloading) everyday! :)

Thank you both for valuable input.

Rob

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
I've implemented the operator overloading on my class as shown below.
Code:
public static bool operator ==(myType c1, myType c2){
  if(c1.AValueProperty != c2.AValueProperty) return false;
  .....
  return true;
}
I've also overrideen Equals method to use this comparison too and the operator !=. This works great for comparing an object to another object which is a different reference of the same type. This doesn't allow null testing any more though as
Code:
if(null == c1) ...;
Now thorws and 'object reference' exception. I've got roudn this by using a try/catch statement and running some code which will throw the error if no object exists but doesnt effect state if one does. This seems a bit scrappy to me as its using try/catch for soemthign it wasn't meant for.

Two questions then.

1) How do I implement null testing in operator overloads?

2) Whats your opinions on using try/catch as a conditional statement where you want to perform some operation if an error is/isn't thrown?

TIA

Rob

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
I would do something like this:
Code:
public static bool operator ==(myType c1, myType c2){

  if ((null == c1) || (null == c2))
    throw new ArgumentNullException("Cannot pass null");

  if (c1.AValueProperty != c2.AValueProperty) 
    return false;
  
  // More comparisons here??

  return true;
}


If you want to get the best response to a question, please check out FAQ222-2244 first
 
The problem is I want to be able to pass null and test to see if the object reference is null appropriately. The above won't let me do that, it just throws a different exception. I was thinking something like
Code:
if(null == c1 && null == c2) return true;
Though that won't work either as any test against myType within the opererator overload will trigger an infinite loop. I don't even think your custom exception would get thrown.

Rob

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
OK, if you want different behavior, that's OK.

BTW, it's not a custom exception -- it's part of the framework. There's nothing that says you can't throw the same exceptions as the framework does.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top