Why variable is not declared?
Normally we must declare variable in a program. Why in this program variable is not declared ?. Problem is highlighted.
using System;
public class SchoolCompare
{
public static void Main()
{
School[] school = new School[8];
// Declaring Objects & declaring an array is two distinct processes. See p70 of the C# Joyce Farrell summary
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;
}
}
/*
Name of School #1 aaa
Students Enrollment Nnuber: 888
Name of School #2 bbb
Students Enrollment Nnuber: 777
Name of School #3 ccc
Students Enrollment Nnuber: 666
Name of School #4 ddd
Students Enrollment Nnuber: 555
Name of School #5 eee
Students Enrollment Nnuber: 444
Name of School #6 fff
Students Enrollment Nnuber: 333
Name of School #7 ggg
Students Enrollment Nnuber: 222
Name of School #8 hhh
Students Enrollment Nnuber: 111
School #1 , Name: hhh, Enrollement Number: 111
School #2 , Name: ggg, Enrollement Number: 222
School #3 , Name: fff, Enrollement Number: 333
School #4 , Name: eee, Enrollement Number: 444
School #5 , Name: ddd, Enrollement Number: 555
School #6 , Name: ccc, Enrollement Number: 666
School #7 , Name: bbb, Enrollement Number: 777
School #8 , Name: aaa, Enrollement Number: 888
*/