-2
namespace Theprogram.cs
{
    class Program
    {
        static void Main(string[] args)
        {
            CreditCustomer[] creditCustomer = new CreditCustomer[5];
            int x, y;
            bool goodNum;
            for (x = 0; x < creditCustomer.Length; ++x)
            {
                creditCustomer[x] = new CreditCustomer();
                Console.Write("Enter customer number ");
                creditCustomer[x].CustomerNumber = Convert.ToInt32(Console.ReadLine());
                goodNum = true;
                for (y = 0; y < x; ++y)
                {
                    if (creditCustomer[x].Equals(creditCustomer[y]))
                        goodNum = false;
                }
                while (!goodNum)
                {
                    Console.Write("Sorry, the customer number " +
                       creditCustomer[x].CustomerNumber + " is a duplicate. " +
                       "\nPlease reenter ");
                    creditCustomer[x].CustomerNumber = Convert.ToInt32(Console.ReadLine());
                    goodNum = true;
                    for (y = 0; y < x; ++y)
                    {
                        if (creditCustomer[x].Equals(creditCustomer[y]))
                            goodNum = false;
                    }
                }
                Console.Write("Enter name ");
                creditCustomer[x].CustomerName = Console.ReadLine();
                Console.Write("Enter age ");
                creditCustomer[x].Rate = Convert.ToInt32(Console.ReadLine());
                Console.Write("Enter amount due ");
                creditCustomer[x].AmountDue = Convert.ToDouble(Console.ReadLine());
            }
            Array.Sort(creditCustomer);
            Array.Sort(customer);
        }
    }
    class Customer : IComparable<Customer>
    {
        private int customerNumber;
        public int CustomerNumber
        {
            get
            {
                return customerNumber;
            }
            set
            {
                customerNumber = value;
            }
        }
        private string customerName;
        public string CustomerName
        {
            get
            {
                return customerName;
            }
            set
            {
                customerName = value;
            }
        }
        private double amountDue;
        public double AmountDue
        {
            get
            {
                return amountDue;
            }
            set
            {
                amountDue = value;
            }
        }
        public Customer(int num, string name, int amt)
        {
        CustomerNumber = num;
        CustomerName = name;
        AmountDue = amt;
        }
        public Customer(): this(9, "ZZZ", 0)
        {
        }
        public override bool Equals(Object e)
        {
            bool equal;
            Customer temp = (Customer)e;
            if (this.CustomerNumber == temp.CustomerNumber)
                equal = true;
            else
                equal = false;
            return equal;
        }
        public override int GetHashCode()
        {
            return CustomerNumber;
        }
        public override string ToString()
        {
            return (GetType() + " Credit Customer " + CustomerNumber + " " + CustomerName +
              " Amount Due is " + AmountDue.ToString("C") + " Interest rate is ");
        }
        protected virtual int IComparable.CompareTo(Object o)
        {
            int returnVal;
            Customer temp = (Customer)o;
            if (this.CustomerNumber >
                temp.CustomerNumber)
                returnVal = 1;
            else
                if (this.CustomerNumber < temp.CustomerNumber)
                    returnVal = -1;
                else
                    returnVal = 0;
            return returnVal;
        }
    }
    class CreditCustomer : Customer, IComparable<CreditCustomer>
    {
        public int Rate {get; set;}
        int MonthlyPayment { get; set; }
        public CreditCustomer(int customerNumber, string customerName, int amountDue, int Rate) : base(customerNumber, customerName, amountDue)
        { }
        public CreditCustomer(): this(0, "", 0, 0)
        { }
        public override string ToString()
        {
            return (GetType() + " Credit Customer " + CustomerNumber + " " + CustomerName +
              " Amount Due is " + AmountDue.ToString("C") + " Interest rate is " + Rate + " Monthly Payment is " + MonthlyPayment);
        }
        int IComparable.CompareTo(object o)
        {
            int returnVal;
            CreditCustomer temp = (CreditCustomer)o;
            if (this.CustomerNumber > temp.CustomerNumber)
                returnVal = 1;
            else
                if (this.CustomerNumber < temp.CustomerNumber)
                    returnVal = -1;
                else
                    returnVal = 0;
            return returnVal;
        }
    }

}

I am having trouble implementing Icomparable in C# for my parent and child classes as I get the error: does not implement interface member'System.IComparable.CompareTo

I really need help on how to implement the Icomparable interface so that I can use the compareTo method on my array of object. Any help would be greatly appreciated.

Lee
  • 142,018
  • 20
  • 234
  • 287
Daniel Robert
  • 15
  • 1
  • 6
  • 1
    You're confusing the generic interface and the non-generic interface. – SLaks Jan 18 '15 at 18:11
  • I am having trouble implementing Icomparable in C# for my parent and child classes as I get the error: does not implement interface member'System.IComparable.CompareTo I really need help on how to implement the Icomparable interface so that I can use the compareTo method on my array of object. Any help would be greatly appreciated. – Daniel Robert Jan 18 '15 at 18:11
  • The error happens on this line in the parent and child classes: int IComparable.CompareTo(object o) – Daniel Robert Jan 18 '15 at 18:12
  • Then this line - class Customer : IComparable says that I didn't implement Icomparable. – Daniel Robert Jan 18 '15 at 18:13

2 Answers2

1

If you're using Visual Studio and are not sure how to implement an interface, just right click IComparable in the class declaration

class CreditCustomer : Customer, IComparable<CreditCustomer>

and select Implement Interface and any of the two options.

The correct signature for the implementation in CreditCustomer would be either:

public int CompareTo(CreditCustomer other)
{
}

or

int IComparable<CreditCustomer>.CompareTo(CreditCustomer other)
{
}

Note that the second signature would hide the method unless you cast the object to IComparable<CreditCustomer>.

Juan
  • 15,274
  • 23
  • 105
  • 187
0

As SLaks mentioned in his comment, there are, in fact, two different interfaces that are named IComparable which can be easily confused:

  1. IComparable is an older, non-generic interface which allows comparison between an object and any other object. There's no type checking - your CreditCustomer can be compared to an int or a List<string> or anything. This interface defines a single method, CompareTo(object obj). This is the method you have in your class.

  2. IComparable<T> is a newer, generic interface. It's very similar to IComparable, but enforces type checking - it only compares T to T, so you don't have to write a lot of boilerplate code making sure that someone didn't try to compare your CreditCustomer to a Dog or something. It also defines a single method - CompareTo(T obj). This is the interface that you marked your class as implementing.

Despite being similarly named, these two are, as far as the compiler is concerned, two different interfaces. If you don't have a CompareTo method that accepts a CreditCustomer argument, you're not implementing IComparable<CreditCustomer>, and that's why it's giving you an error. Either change your method signature to match the interface, or change the interface you're using to one that matches the method signature. Probably the former.

Avner Shahar-Kashtan
  • 14,492
  • 3
  • 37
  • 63