Following program is all about sorting arrays of objects. It is comparing father's age and my age.
I couldn't understand what is Temp.Age. Anyone knows please explain.
using System;
public class Person : IComparable
{
public Person() { }
// private fields
private string _FirstName;
private string _LastName;
private int _Age;
public Person(string FirstName, string LastName, int Age)
{
this.FirstName = FirstName;
this.LastName = LastName;
this.Age = Age;
}
//Properties
public string FirstName
{
get { return _FirstName; }
set { _FirstName = value; }
}
public string LastName
{
get { return _LastName; }
set { _LastName = value; }
}
public int Age
{
get { return _Age; }
set { _Age = value; }
}
//This was the interface member
public int CompareTo(object obj)
{
Person Temp = (Person)obj;
if (this.Age < Temp.Age) //????????????????????????
{return 1;}
if (this.Age > Temp.Age)
{return -1;}
else
{return 0;}
}
}
//The method int CompareTo (object obj) returns an integer. You can specify your result according to the returned value as follow:
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Person me = new Person("Bejaoui", "Bechir", 29);
Person myFather = new Person("Bejaoui", "Massinissa", 65);
Person myGrandFather = new Person("Krayem", "Shishaq", 95);
int state = me.CompareTo(myFather);
if (state == 1) Console.WriteLine("My father is older than me");
if (state == -1) Console.WriteLine("I'm older than my father!!!");
if (state == 0) Console.WriteLine("My Father and I have the same age!");
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
/*
Hello World!
My father is older than me
*/
Loading
VulpesRank #55 · 29.2K reputationPosted Dec 17, 2011, 8:40 AM EST
It therefore first needs to be cast to Person and stored in the local variable Temp.
You can then access its Age property and compare it with the Age property of the current Person object (i.e. this) for sorting purposes.
Posted Dec 17, 2011, 10:06 AM EST
VulpesRank #55 · 29.2K reputationPosted Dec 17, 2011, 10:32 AM EST
Posted Dec 17, 2011, 12:49 PM EST
using System;
public class ComparableEmployeeArray_idNumber
{
public static void Main()
{
Console.WriteLine("SORTING idNumber IN ACENDING ORDER");
Console.WriteLine();
Employee[] emp = new Employee[] { new Employee(333, 12.50), new Employee(111, 14.75), new Employee(222, 22.35) };
for (int x = 0; x < emp.Length; x++)
Console.WriteLine("Employee #{0} ID = {1}, Salary = {2}", x + 1, emp[x].GetId(), emp[x].GetSalary().ToString("c2"));
Console.WriteLine("\nSorted employees: ");
Console.WriteLine();
Array.Sort(emp);
for (int x = 0; x < emp.Length; x++)
Console.WriteLine("Employee #{0} ID = {1}, Salary = {2}", x + 1, emp[x].GetId(), emp[x].GetSalary().ToString("c2"));
Console.ReadKey();
}
}
class Employee : IComparable
{
private int idNumber;
private double salary;
public Employee(int id, double pay)
{
idNumber = id;
salary = pay;
}
public int GetId()
{
return idNumber;
}
public double GetSalary()
{
return salary;
}
public int CompareTo(Employee emp)
{
int returnVal;
if (this.idNumber > emp.idNumber)
//tem.idNumber - compared object
returnVal = 1;
//This instance is greater than the compared object.
else
if(this.idNumber
returnVal = -1;
//This instance is less than the compared object.
else
returnVal = 0;
//This instance is equal to the compared object.
return returnVal;
}
}
/*
SORTING idNumber IN ACENDING ORDER
Employee #1 ID = 333, Salary = $12.50
Employee #2 ID = 111, Salary = $14.75
Employee #3 ID = 222, Salary = $22.35
Sorted employees:
Employee #1 ID = 111, Salary = $14.75
Employee #2 ID = 222, Salary = $22.35
Employee #3 ID = 333, Salary = $12.50
*/
Sam HobbsRank #108 · 20.5K reputationPosted Dec 17, 2011, 1:06 PM EST
For what it is worth, generic collections are generic classes. Generic classes can exist for many other purposes, not just for collections. Generic classes in C# are essentially the same thing as templates in C++. I think that the term template is more indicative of what they are. So you could say that a generic collection is a template collection in the sense that the class is a template that can be used for a type or types that are specified in the declaration.
VulpesRank #55 · 29.2K reputationPosted Dec 17, 2011, 1:07 PM EST
VulpesRank #55 · 29.2K reputationPosted Dec 17, 2011, 1:47 PM EST
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts