Mega Code Archive

 
Categories / C# / Language Basics
 

Delegates And Events

/* Learning C#  by Jesse Liberty Publisher: O'Reilly  ISBN: 0596003765 */  using System;  namespace DelegatesAndEvents  {      public enum comparison      {          theFirstComesFirst = 1,          theSecondComesFirst = 2      }      // A simple collection to hold two items      class Pair      {          // Private array to hold the two objects          private object[] thePair = new object[2];          // The delegate declaration          public delegate comparison              WhichIsFirst(object obj1, object obj2);          // Passed in constructor takes two objects,          // added in order received          public Pair(              object firstObject,              object secondObject)          {              thePair[0] = firstObject;              thePair[1] = secondObject;          }          // Public method that orders the          // two objects by whatever criteria the objects like!          public void Sort(              WhichIsFirst theDelegatedFunc)          {              if (theDelegatedFunc(thePair[0],thePair[1])                  == comparison.theSecondComesFirst)              {                  object temp = thePair[0];                  thePair[0] = thePair[1];                  thePair[1] = temp;              }          }          // Public method that orders the          // two objects by the reverse of whatever criteria the          // objects likes!          public void ReverseSort(              WhichIsFirst theDelegatedFunc)          {              if (theDelegatedFunc(thePair[0],thePair[1]) ==                  comparison.theFirstComesFirst)              {                  object temp = thePair[0];                  thePair[0] = thePair[1];                  thePair[1] = temp;              }          }          // Ask the two objects to give their string value          public override string ToString()          {              return thePair[0].ToString() + ", "                  + thePair[1].ToString();          }      }      class Dog      {          private int weight;          public Dog(int weight)          {              this.weight=weight;          }          // dogs are sorted by weight          public static comparison WhichDogComesFirst(              Object o1, Object o2)          {              Dog d1 = (Dog) o1;              Dog d2 = (Dog) o2;              return d1.weight > d2.weight ?                  comparison.theSecondComesFirst :                  comparison.theFirstComesFirst;          }          public override string ToString()          {              return weight.ToString();          }      }      class Student      {          private string name;          public Student(string name)          {              this.name = name;          }          // Students are sorted alphabetically          public static comparison              WhichStudentComesFirst(Object o1, Object o2)          {              Student s1 = (Student) o1;              Student s2 = (Student) o2;              return (String.Compare(s1.name, s2.name) < 0 ?                  comparison.theFirstComesFirst :                  comparison.theSecondComesFirst);          }          public override string ToString()          {              return name;          }      }     public class TesterDelegatesAndEvents11     {        public void Run()        {            // Create two students and two dogs            // and add them to Pair objects            Student Jesse = new Student("Jesse");            Student Stacey = new Student ("Stacey");            Dog Milo = new Dog(65);            Dog Fred = new Dog(12);            Pair studentPair = new Pair(Jesse,Stacey);            Pair dogPair = new Pair(Milo, Fred);            Console.WriteLine("studentPair\t\t\t: {0}",                studentPair.ToString());            Console.WriteLine("dogPair\t\t\t\t: {0}",                dogPair.ToString());            // Instantiate the delegates            Pair.WhichIsFirst  theStudentDelegate =                new Pair.WhichIsFirst(                Student.WhichStudentComesFirst);            Pair.WhichIsFirst theDogDelegate =                new Pair.WhichIsFirst(                Dog.WhichDogComesFirst);            // Sort using the delegates            studentPair.Sort(theStudentDelegate);            Console.WriteLine("After Sort studentPair\t\t: {0}",                studentPair.ToString());            studentPair.ReverseSort(theStudentDelegate);            Console.WriteLine("After ReverseSort studentPair\t: {0}",                studentPair.ToString());            dogPair.Sort(theDogDelegate);            Console.WriteLine("After Sort dogPair\t\t: {0}",                dogPair.ToString());            dogPair.ReverseSort(theDogDelegate);            Console.WriteLine("After ReverseSort dogPair\t: {0}",                dogPair.ToString());        }        static void Main()        {           TesterDelegatesAndEvents11 t = new TesterDelegatesAndEvents11();           t.Run();        }     }  }