Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / July 2007

Tip: Looking for answers? Try searching our database.

operator overloading

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
GeezerButler - 08 Jul 2007 23:10 GMT
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.
Peter Duniho - 08 Jul 2007 23:31 GMT
> 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"
[quoted text clipped - 5 lines]
> 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
GeezerButler - 09 Jul 2007 00:25 GMT
On Jul 9, 3:31 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.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"
[quoted text clipped - 23 lines]
>
> 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
Peter Duniho - 09 Jul 2007 00:40 GMT
> [...]
> 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
GeezerButler - 09 Jul 2007 01:32 GMT
> 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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.