Re: operator overloading by GeezerButler
GeezerButler
Sun Jul 08 18:25:27 CDT 2007
On Jul 9, 3:31 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> On Sun, 08 Jul 2007 15:10:57 -0700, GeezerButler <kurtr...@gmail.com>
> wrote:
>
> > 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
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