Hi Guys
NP41 Why Casting
Following program is a book exercise. IComparable.CompareTo() method included to sort school objects by enrollment. Here there is a casting, I couldn’t uderstand the need for casting here. Any one know please explain.
School temp = (School)o;
Thank you
using System;
public class SchoolCompare
{
public static void
{
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;
}
}
Posted Sep 13, 2007, 2:54 PM
Thank you very much for your explanation Alan
AlanPosted Sep 13, 2007, 2:32 PM
I imagine you're now getting a 'use of unassigned local variable returnVal' error.
The difference is that, previously, the compiler could be sure that whatever path it took through the code, returnVal would have been assigned to before it was returned from the method.
This is no longer the case even though, to the human reader, returnVal must have been assigned to because this.enrolled must be greater than, less than or equal to temp.enrolled. There are no other possibilities.
The compiler isn't clever enough to see this and it just looks as though, if all three conditions turned out to be false, then returnVal would not have been assigned to - hence the errror.
Posted Sep 13, 2007, 1:45 PM
In the above program implementation in CompareTo() method has been modified. Two else statements are comment out and new statement if (this.enrolled == temp.enrolled)has been introduced but program is producing error result.
Please any one explain why it is wrong to modify such way.
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;
if (this.enrolled == temp.enrolled)
//else
returnVal = 0;
return returnVal;
}
Posted Sep 12, 2007, 10:43 AM
Thank you very much for your help Alan
AlanPosted Sep 11, 2007, 12:07 PM
The reason for the cast is because 'o' is a variable of type Object even though at runtime you're going to assign a reference to a School object to it.
In general, you can't just assign an Object variable to a School variable and so you need to use a cast to tell the compiler that the object variable will contain a School object at runtime. If it doesn't in fact contain such an object, then the application will throw an 'invalid cast' exception.