PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 1.00 average.

Confused by the CompareTo method of IComparable

 
 
Author
Guest
Posts: n/a
 
      10th Jul 2008
I understand that if we wanna compare two objects of the same class,
we need to implement the IComparable interface, as shown here:

http://msdn.microsoft.com/en-us/library/system.icomparable.compareto(printer).aspx

I am confused by the implementation of the CompareTo method in the C#
example on the above page. Look:

public int CompareTo(object obj)
{
if(obj is Temperature)
{
Temperature otherTemperature = (Temperature) obj;
return
this.temperatureF.CompareTo(otherTemperature.temperatureF);
}
else
{
throw new ArgumentException("object is not a Temperature");
}
}

The thing that baffles me is that the implementation of CompareTo
makes a call to this method itself. Hey, CompareTo is *not*
implemented in the interface (no interfaces implements a method), and
*we* are trying to implement it, then how are we able to call this
method in order to implement it? I don't understand the logic here.

Instead, I thought that the implementation of CompareTo in this case
should really be this:

public int CompareTo(object obj)
{
if(obj is Temperature)
{
Temperature otherTemperature = (Temperature) obj;
if (this.temperatureF < otherTemperature)
{ return -1; }
else if ((this.temperatureF == otherTemperature)
{ return 0; }
else
{ return 1; }
}
else
{
throw new ArgumentException("object is not a Temperature");
}
}

Dear gurus, so what is the reason behind calling CompareTo itself when
implementing it? Thank you.
 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      10th Jul 2008
Author <(E-Mail Removed)> wrote:
> I understand that if we wanna compare two objects of the same class,
> we need to implement the IComparable interface, as shown here:
>
> http://msdn.microsoft.com/en-us/library/system.icomparable.compareto(printer).aspx
>
> I am confused by the implementation of the CompareTo method in the C#
> example on the above page. Look:
>
> public int CompareTo(object obj)
> {
> if(obj is Temperature)
> {
> Temperature otherTemperature = (Temperature) obj;
> return
> this.temperatureF.CompareTo(otherTemperature.temperatureF);
> }
> else
> {
> throw new ArgumentException("object is not a Temperature");
> }
> }
>
> The thing that baffles me is that the implementation of CompareTo
> makes a call to this method itself.


Where? It looks to me like it's calling double's implementation of
CompareTo.

--
Jon Skeet - <(E-Mail Removed)>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
 
Reply With Quote
 
Author
Guest
Posts: n/a
 
      10th Jul 2008
On Jul 10, 3:08*pm, Jon Skeet [C# MVP] <sk...@pobox.com> wrote:
> Author <gnewsgr...@gmail.com> wrote:
> > I understand that if we wanna compare two objects of the same class,
> > we need to implement the IComparable interface, as shown here:

>
> >http://msdn.microsoft.com/en-us/library/system.icomparable.compareto(...

>
> > I am confused by the implementation of the CompareTo method in the C#
> > example on the above page. *Look:

>
> > public int CompareTo(object obj)
> > {
> > * * * * if(obj is Temperature)
> > * * * * {
> > * * * * * * Temperature otherTemperature = (Temperature) obj;
> > * * * * * * return
> > this.temperatureF.CompareTo(otherTemperature.temperatureF);
> > * * * * }
> > * * * * else
> > * * * * {
> > * * * * * *throw new ArgumentException("object is not a Temperature");
> > * * * * }
> > }

>
> > The thing that baffles me is that the implementation of CompareTo
> > makes a call to this method itself.

>
> Where? It looks to me like it's calling double's implementation of
> CompareTo.
>
> --
> Jon Skeet - <sk...@pobox.com>
> Web site:http://www.pobox.com/~skeet*
> Blog:http://www.msmvps.com/jon_skeet
> C# in Depth:http://csharpindepth.com


That's right. So, double actually implements CompareTo like what I
did. That must be it. Thank you.
 
Reply With Quote
 
Author
Guest
Posts: n/a
 
      10th Jul 2008
On Jul 10, 3:08*pm, Jon Skeet [C# MVP] <sk...@pobox.com> wrote:
> Author <gnewsgr...@gmail.com> wrote:
> > I understand that if we wanna compare two objects of the same class,
> > we need to implement the IComparable interface, as shown here:

>
> >http://msdn.microsoft.com/en-us/library/system.icomparable.compareto(...

>
> > I am confused by the implementation of the CompareTo method in the C#
> > example on the above page. *Look:

>
> > public int CompareTo(object obj)
> > {
> > * * * * if(obj is Temperature)
> > * * * * {
> > * * * * * * Temperature otherTemperature = (Temperature) obj;
> > * * * * * * return
> > this.temperatureF.CompareTo(otherTemperature.temperatureF);
> > * * * * }
> > * * * * else
> > * * * * {
> > * * * * * *throw new ArgumentException("object is not a Temperature");
> > * * * * }
> > }

>
> > The thing that baffles me is that the implementation of CompareTo
> > makes a call to this method itself.

>
> Where? It looks to me like it's calling double's implementation of
> CompareTo.
>
> --
> Jon Skeet - <sk...@pobox.com>
> Web site:http://www.pobox.com/~skeet*
> Blog:http://www.msmvps.com/jon_skeet
> C# in Depth:http://csharpindepth.com


I had thought it was this.CompareTo(that.temperature).
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What's wrong with "CompareTo" method? Curious Microsoft Dot NET 2 17th Aug 2009 02:09 PM
String Comparison in "CompareTo" Method Fir5tSight Microsoft Dot NET 3 23rd Aug 2006 03:50 PM
Confused with ExecuteScalar method of SQLCommand =?Utf-8?B?RGluZXNo?= Microsoft C# .NET 2 22nd Mar 2005 07:59 PM
IComparable.CompareTo not being called on Array.Reverse Brian W Microsoft C# .NET 1 2nd Jul 2004 12:16 AM
String.CompareTo() method ; Strange behavior Kapil Sachdeva Microsoft Dot NET Framework 1 7th Apr 2004 07:12 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:10 AM.