The problem
The problem: It crashes on me!!! The problem highlighted in red color, it tells me "Index was outside the bounds of the array", I know what it means, but i cant find the problem....
My Solution:
static void Main(string[] args)
{
School[] schools = new School[5];
string schoolName;
int schoolStudentAmount;
Console.WriteLine("Please enter information about 5 school: ");
for (int index = 0; index < 10; index++)
{
Console.WriteLine("Details for each school: ", index + 1);
Console.WriteLine(" Enter school name : ");
schoolName = Console.ReadLine();
Console.WriteLine("Enter amount of students in the school: ");
schoolStudentAmount = Convert.ToInt32(Console.ReadLine());
schools[index] = new School(schoolName,schoolStudentAmount);
}
Console.WriteLine("The schools and amount of students in it");
for (int index = 0; index < 10; index++)
Console.WriteLine(schools[index]);
Array.Sort(schools);
Console.WriteLine("The schools and enrolled students sorted from smallest to largest");
for (int index = 0; index < 6; index++)
Console.WriteLine(schools[index]);
====================
private string schoolName;
private int schoolStudentsAmount;
public string SchoolName
{
get { return schoolName; }
set { schoolName = value; }
}
public int SchoolStudentsAmount
{
get { return schoolStudentsAmount; }
set { schoolStudentsAmount = value; }
}
public School(string schoolName, int schoolStudentsAmount)
{
this.schoolName = schoolName;
this.schoolStudentsAmount = schoolStudentsAmount;
}
public override string ToString()
{
return String.Format("School name {0}, amount of students {1}.", schoolName, schoolStudentsAmount);
}
public int CompareTo(School other)
{
return this.schoolStudentsAmount.CompareTo(other.schoolStudentsAmount);
}