question

MarkGreen-2360 avatar image
0 Votes"
MarkGreen-2360 asked MarkGreen-2360 answered

Visual C#: Can I call the CompareTo method required by the IComparable interface?

Can I call the CompareTo() method required by the IComparable interface or is it only available to the System? In the following code, I can call the .Sort() method which uses the Icomparable<T>.CompareTo method that I wrote but I cannot call the .CompareTo method. Do I just have the wrong syntax?

When I try to type in the line to call .CompareTo, Visual Studio shows a red, squiggly line under CompareTo with this message:
"CS1061: 'Program.Person' does not contain a definition for 'CompareTo' and no accessible extension method 'CompareTo' accepting a first argument of type 'Program.Person' could be found"

 class Program
 {
     public class Person : IComparable<Person>
     {
         public string sName;
         public int iAge;

         public Person(string name, int age)
         {
             this.sName = name;
             this.iAge = age;
         }

         int IComparable<Person>.CompareTo(Person otherPerson)
         {
             // check if other person is a valid object reference
             if (otherPerson == null)
             {
                 if (this == null) return 0;     // if both persons are null, return EQUAL
                 return 1;                       // if only other person is null, this person is greater
             }

             if (this == null) return -1;        // if only this person is null, it is lesser

            if (this.sName != otherPerson.sName) return String.Compare(this.sName, otherPerson.sName);
             return this.iAge - otherPerson.iAge;
         }
     }

     static void Main(string[] args)
     {
         List<Person> People = new List<Person>();

         People.Add(new Person("Joe", 25));
         People.Add(new Person("Clark", 36));
         People.Add(new Person("Lois", 33));

         // I can call the .Sort() method so the System can access my .CompareTo() method
         People.Sort();

         // Visual Studio does not accept this line. Error CS1061
         if(People[0].CompareTo(People[1]) < 0)
         {
             // do something
         }

         Person Person1 = new Person("Bill", 28);
         Person Person2 = new Person("Becky", 24);

         // Visual Studio does not accept this line either. Error CS1061
         if (Person1.CompareTo(Person2) < 0)
         {
             // do something
         }
     }
dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered Viorel-1 edited

If you change the definition to:

 public int CompareTo( Person otherPerson )
 {
    . . .
 }

then the errors will disappear.

If you do not want such change, keeping “explicit interface implementation”, then you have to write something like this:

 if( ( (IComparable<Person>)People[0] ).CompareTo( People[1] ) < 0 )
 {
    // do something
 }

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

Have you read the docs and looked at the samples?

Microsoft docs, simple code sample.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MarkGreen-2360 avatar image
0 Votes"
MarkGreen-2360 answered

Thank you very much for your replies.

karenpayneoregon, yes I searched at length and looked at many examples but could not find how to call the CompareTo() method myself as opposed to the system using it for .Sort(). However, my problem was not how I called the CompareTo() method. It was how I declared it.

Resources I looked at showed that I should use this syntax to satisfy the requirement by the IComparable interface to provide a CompareTo() method:

int IComparable<Person>.CompareTo(Person otherPerson)

Based on your responses, I think this is incorrect and that the following syntax will satisfy the IComparable interface's requirement for a CompareTo() method:

int CompareTo(Person otherPerson)

My problem was that I was given the wrong syntax for declaring the CompareTo() method. My syntax for calling the CompareTo() method was fine.

Thanks again for your help!

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.