Maha

Maha

  • NA
  • 0
  • 169.7k

Interface

May 9 2014 9:07 AM
In this program we are using IComparable interface but we didn't declare interface as such

public interface IComparable
{
        int CompareTo(object o);
}

I assume that once we implemented the interface we didn't need declaration that is way declaration is not there in this program. This is true for any programs. I wish to know whether my understanding is correct. Problem is highlighted.

using System;

public class SchoolCompare
{
public static void Main()
{
School[] school = new School[8];

for (int x = 0; x < school.Length; ++x)
{
school[x] = new School();

Console.Write("Name of School #{0} ", x + 1);

school[x].SchoolName = Console.ReadLine();

Console.Write("Students Enrollment Nnuber: ");

school[x].StudentEnrolled = Convert.ToInt32(Console.ReadLine());

Console.WriteLine();
}

Array.Sort(school);

for (int x = 0; x < school.Length; ++x)

Console.WriteLine("School #{0} , Name: {1}, Enrollement Number: {2}",
x + 1, school[x].SchoolName, school[x].StudentEnrolled);
}
}
class School : IComparable
{
private string sName;
private int enrolled;

public string SchoolName
{
get { return sName; }
set { sName = value; }
}
public int StudentEnrolled
{
get { return enrolled; }
set { enrolled = value; }
}

public int CompareTo(object o)
{
int returnVal;

School temp = (School)o;

if (this.enrolled > temp.enrolled)
returnVal = 1;
else
if (this.enrolled < temp.enrolled)
returnVal = -1;
else
returnVal = 0;

return returnVal;
}
}


Answers (4)