operator overloading

  • Thread starter Thread starter GeezerButler
  • Start date Start date
G

GeezerButler

I have 2 objects which could be of any type (but both are of same
type)
I would like to check if it has the "greater than" or "less than"
operator overloaded. If it does, then i would like to compare the 2
objects using the operators. Is this possible to do?

In general, i am solving this problem, "If 2 objects can be compared,
return their comparison value".
I can check if they implement IComparer/IComparable but i don't know
how to proceed after that.
 
I have 2 objects which could be of any type (but both are of same type)
I would like to check if it has the "greater than" or "less than"
operator overloaded. If it does, then i would like to compare the 2
objects using the operators. Is this possible to do?

In general, i am solving this problem, "If 2 objects can be compared,
return their comparison value".
I can check if they implement IComparer/IComparable but i don't know
how to proceed after that.

You may need to clarify the question.

Whether a class implements IComparable is not the same as whether it has
overloaded the > or < operators. Of course, having done one, the other is
usually fairly easy as well. But they aren't equivalent. It's not really
clear what functionality you actually need here. Do you want to use
IComparable? Or do you want to use overloaded operators?

Assuming you have already determined a class instance implements
IComparable, then all you need to do is cast it to IComparable and call
IComparable.CompareTo() with the instance:

IComparable compare = (IComparable)instance1;

return compare.CompareTo(instance2);

Pete
 
You may need to clarify the question.

Whether a class implements IComparable is not the same as whether it has
overloaded the > or < operators. Of course, having done one, the other is
usually fairly easy as well. But they aren't equivalent. It's not really
clear what functionality you actually need here. Do you want to use
IComparable? Or do you want to use overloaded operators?

Assuming you have already determined a class instance implements
IComparable, then all you need to do is cast it to IComparable and call
IComparable.CompareTo() with the instance:

IComparable compare = (IComparable)instance1;

return compare.CompareTo(instance2);

Pete

Yes the IComaprer/IComparable part is fine.
But what if the object does not implement IComparer. The next step to
check if the objects can be comapred is to check whether they overload
the less/greater than operator.
So, given the Type of the object, I need to find if it does overload
the less/greater than operator. And then call the operator to compare
the objects.
Is this sort of a thing possible to do?

Regards
Geezer
 
[...]
So, given the Type of the object, I need to find if it does overload
the less/greater than operator. And then call the operator to compare
the objects.
Is this sort of a thing possible to do?

I assume you could do it via reflection. You'd have to check for the
implementation of the overloaded operators ("operator<" and "operator>"),
and then I think you would also need to use reflection to actually use it.

However, there's a pretty good chance that whatever you're trying to do is
incorrect, if it involves using reflection. Going down the road to
reflection is usually a sign that there's something else fundamentally
broken about the architecture of the code, and that you are trying to
solve problems that were the code designed better would not have to be
solved at all.

There are always exceptions to the rule, of course, but it's something you
should think about.

As far as more specific advice goes, unless you post more specifics about
what's going on in your code and your design, I think that's about all the
advice I know to offer.

Pete
 
I assume you could do it via reflection. You'd have to check for the
implementation of the overloaded operators ("operator<" and "operator>"),
and then I think you would also need to use reflection to actually use it.
Actually i was looking for the exact method to do this. <sheepish
smile>
I have found out how to do it now :)
However, there's a pretty good chance that whatever you're trying to do is
incorrect, if it involves using reflection.
Yeah in general that's true, but my requirememnts (I am supposed to
compare any .NET or custom type) are such that reflection is the way
to go. Atleast that's how it seems to me.
 
Back
Top