Wrox Programmer Forums
|
C# 2012/5.0 General Discussion Discussions about the C# 2012 language and tool not related to any specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2012/5.0 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 1st, 2014, 11:32 AM
Authorized User
 
Join Date: Nov 2012
Posts: 25
Thanks: 3
Thanked 0 Times in 0 Posts
Question Using CompareTo() Interface

Could some one tell me why I am getting a syntax error when I try to implement the CompareTo() interface. I am getting an error which seems to say that It could not find my code for the compareTo() method in my school class. I think I am doing something wrong in terms of inheritance. If I could just fix the syntax error I could get the rest of the code to work.

The program below accepts 5 school objects. Each object takes the name of the school and the enrollment size. I am suppose to sort each school by enrollment using the CompareTo() Method which is suppose to be implemented using the ICompare interface. The code is below:

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chapt9_Exe8A
//this is  my school class which implements the IComparable interface
    class School: IComparable
    {
        public string name {get;set;}
        public int enrollSize {get;set;}

        public School() 
        {
            name = null;
            enrollSize = 0;
        }
        public School(string name, int enrollSize) 
        {
            this.name = name;
            this.enrollSize = enrollSize;
        }

/*This is the CompareTo() method implementation it returns integer. 1 if this.enroll is larger than what is passed in the CompareTo method, -1 if this is less, and 0 if they are the same*/
 
        public int IComparable.CompareTo(School s) 
        {
            int returnVal;
            School temp = s;
            if (this.enrollSize > temp.enrollSize)
                returnVal = 1;
            else
                if (this.enrollSize < temp.enrollSize)
                    returnVal = -1;
                else
                    returnVal = 0;
            return returnVal;
        }
    }
   
/*This is my SchoolDemo class.  It accepts the required information form the user and builds the five objects.  It is suppose to display the information in the correct order but, I've haven't got around to coding that yet.*/

    class SchoolsDemo: School
    {
        static void Main(string[] args)
        {
            School[] schoolArray = new School [5];
            School schoolTemp = new School();
            Console.WriteLine("This program will ask you for the name and enrollment of five schools"
                + "then sort you input");
            for (int i = 0; i <= 5; ++i)
            {
                Console.WriteLine("Please enter name of {0}st school: ", i + 1);
                string name = Console.ReadLine();
                Console.WriteLine("Please enter enrollment size: ");
                int enrollSize = Convert.ToInt32(Console.ReadLine());
                schoolArray [i] = new School (name, enrollSize);
                
            }
            int b = 1;
            for (int a =0; a < 5; ++a){
                ++b;
            for (int i = 0; i < 5; ++i)
            {
                int answer = schoolArray[a].CompareTo(schoolArray[i+b]);
                if (answer == 1) 
                { 
                    schoolTemp = schoolArray[i+b];
                        schoolArray[i+b] = schoolArray[a];
                            schoolArray[a] = schoolTemp;
                            if ((i + b) == 4)
                                break;
                }
            }
            }

            
        }
    }
}
 
Old January 1st, 2014, 11:47 AM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

Quote:
Originally Posted by Truck35 View Post
Could some one tell me why I am getting a syntax error when I try to implement the CompareTo() interface. I am getting an error which seems to say that It could not find my code for the compareTo() method in my school class. I think I am doing something wrong in terms of inheritance. If I could just fix the syntax error I could get the rest of the code to work.

The program below accepts 5 school objects. Each object takes the name of the school and the enrollment size. I am suppose to sort each school by enrollment using the CompareTo() Method which is suppose to be implemented using the ICompare interface. The code is below:

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chapt9_Exe8A
//this is  my school class which implements the IComparable interface
    class School: IComparable
    {
        public string name {get;set;}
        public int enrollSize {get;set;}

        public School() 
        {
            name = null;
            enrollSize = 0;
        }
        public School(string name, int enrollSize) 
        {
            this.name = name;
            this.enrollSize = enrollSize;
        }

/*This is the CompareTo() method implementation it returns integer. 1 if this.enroll is larger than what is passed in the CompareTo method, -1 if this is less, and 0 if they are the same*/
 
        public int IComparable.CompareTo(School s) 
        {
            int returnVal;
            School temp = s;
            if (this.enrollSize > temp.enrollSize)
                returnVal = 1;
            else
                if (this.enrollSize < temp.enrollSize)
                    returnVal = -1;
                else
                    returnVal = 0;
            return returnVal;
        }
    }

}
Your CompareTo method doesn't implement the interface IComparable.
The interface IComparable is the non-generic version, with this you need to implement a method that receives an object parameter. In case you use the generic version, IComparable<School>, you can have a method implementation with the School parameter. Just use "go to definition" in the editor to have a look at the interface members. Also, you could use "implement interface" to have the correct signature for the members of the interface implemented.
Another issue: you can either explicit or implicit implement the interface. Your implementation is partly one of each variant.

I hope this helps.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
The Following User Says Thank You to ChristianNagel For This Useful Post:
Truck35 (January 1st, 2014)





Similar Threads
Thread Thread Starter Forum Replies Last Post
What is interface? smfarooq C# 5 May 7th, 2013 02:18 AM
Interface morteza ASP.NET 4 General Discussion 0 February 8th, 2011 12:43 AM
Interface creation pratik28 ASP.NET 2.0 Basics 1 May 8th, 2009 08:08 AM
interface inheritance draganmit VB.NET 2002/2003 Basics 6 April 29th, 2004 09:40 AM
Regarding Interface ndr1977 BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 1 July 30th, 2003 09:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.