Delegates in .NET
i am just trying to understand delegates in .Net, I am using Professional C# wrox book for this example
I created this .dll and i refrenced in DelegateTest project but i am running into compilation error, Do you see where i gone wrong?
Thanks in advance for your help.
//************************************************** *****************
//****** This is the dll that i am using as Library ***************
using System;
namespace DelegateLib
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class BubbleSorter
{
public delegate bool CompareOp(object lhs, object rhs);
static public void Sort(object [] sortArray, CompareOp gtMethod)
{
for (int i=0 ; i<sortArray.Length ; i++)
{
for (int j=i+1 ; j<sortArray.Length ; j++)
{
if (gtMethod(sortArray[j], sortArray[i]))
{
object temp = sortArray[i];
sortArray[i] = sortArray[j];
sortArray[j] = temp;
}
}
}
}
}
}
//************************************************** ******************
//************************************************** ******************
//********** This is the client i am using for testing *************
using System;
using DelegateLib;
namespace DelegateTest
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class DelegateTesting
{
delegate bool CompareOpTest(object lhs, object rhs);
static void Main()
{
Employee [] employees =
{
new Employee("Karli Watson", 20000),
new Employee("Bill Gates", 10000),
new Employee("Simon Robinson", 25000),
new Employee("Mortimer", (decimal)1000000.38),
new Employee("Arabel Jones", 23000),
new Employee("Avon from 'Blake's 7'", 50000)};
CompareOpTest employeeCompareOp = new CompareOpTest(Employee.RhsIsGreater);
BubbleSorter.Sort(employees, employeeCompareOp);
for (int i=0 ; i<employees.Length ; i++)
Console.WriteLine(employees[i].ToString());
Console.ReadLine();
}
}
class Employee // : object
{
private string name;
private decimal salary;
public Employee(string name, decimal salary)
{
this.name = name;
this.salary = salary;
}
public override string ToString()
{
return string.Format(name + ", {0:C}", salary);
}
public static bool RhsIsGreater(object lhs, object rhs)
{
Employee empLhs = (Employee) lhs;
Employee empRhs = (Employee) rhs;
return (empRhs.salary > empLhs.salary) ? true : false;
}
}
}
//************************************************** ******************
|