Start New Discussion within our Software Development Community

//am having errors on this portion of my code
// am require to implement Icomparable interface with my rational class

// 
   
  public int CompareTo(Rational opnd1,Rational opnd2 )
         {
             if (Rational opnd1 < Rational opnd2)
             return -1;
             else (Rational opnd1 == Rational opnd2)
             return 0;
             else 
             return 1;
using System;

class Program
{
    static void Main(string[] args)
    {
        Foo foo = new Foo();
        foo.Blah = 17;

        Foo foo2 = new Foo();
        foo2.Blah = 36;

        foo.CompareTo(foo2); // uses IComparable<Foo> implementation

        ((IComparable)foo).CompareTo(foo2); // uses IComparable implementation
    }
}

public class Foo : IComparable<Foo>, IComparable
{
    public int Blah { get; set; }

    #region IComparable<Foo> Members

    public int CompareTo(Foo other)
    {
        return this.Blah.CompareTo(other.Blah);
    }

    #endregion

    #region IComparable Members

    int IComparable.CompareTo(object obj)
    {
        return this.CompareTo((Foo)obj);
    }

    #endregion
}

Here's for another round of "getting to know your IDE." When you inherit from an interface, after you type in the name of the interface a blue bar will appear under the first character of the interface name. Hover over it and you'll get a drop down that allows you to create the implementation shell of the interface's members. You'll get the right signatures, all you need to do at that point is put in the proper implementation. (Your signature above was incorrect from the start, it looks like you are going for the similarly named IComparer interface instead.)

Attachments IDE_Interface_1.png 1.87 KB IDE_Interface_2.png 6.76 KB IDE_Interface_3.png 6.46 KB
This article has been dead for over six months. Start a new discussion instead.