OKIEWARDOYO

C#.NET No.9-4

Dec 19th, 2012
5,134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.38 KB | None | 0 0
  1. using System;
  2.  
  3. // this is the delegate declaration
  4. public delegate int Comparer(object obj1, object obj2);
  5.  
  6. public class Name
  7. {
  8.     public string FirstName = null;
  9.     public string LastName = null;
  10.  
  11.     public Name(string first, string last)
  12.     {
  13.         FirstName = first;
  14.         LastName = last;
  15.     }
  16.  
  17.     // this is the delegate method handler
  18.     public static int CompareFirstNames(object name1, object name2)
  19.     {
  20.         string n1 = ((Name)name1).FirstName;
  21.         string n2 = ((Name)name2).FirstName;
  22.  
  23.         if (String.Compare(n1, n2) > 0)
  24.         {
  25.             return 1;
  26.         }
  27.         else if (String.Compare(n1, n2) < 0)
  28.         {
  29.             return -1;
  30.         }
  31.         else
  32.         {
  33.             return 0;
  34.         }
  35.     }
  36.  
  37.     public override string ToString()
  38.     {
  39.         return FirstName + " " + LastName;
  40.     }
  41. }
  42.  
  43. class SimpleDelegate
  44. {
  45.     Name[] names = new Name[5];
  46.  
  47.     public SimpleDelegate()
  48.     {
  49.         names[0] = new Name("Joe", "Mayo");
  50.         names[1] = new Name("John", "Hancock");
  51.         names[2] = new Name("Jane", "Doe");
  52.         names[3] = new Name("John", "Doe");
  53.         names[4] = new Name("Jack", "Smith");
  54.     }
  55.  
  56.     static void Main(string[] args)
  57.     {
  58.         SimpleDelegate sd = new SimpleDelegate();
  59.  
  60.         // this is the delegate instantiation
  61.         Comparer cmp = new Comparer(Name.CompareFirstNames);
  62.  
  63.         Console.WriteLine("\nBefore Sort: \n");
  64.  
  65.         sd.PrintNames();
  66.  
  67.         // observe the delegate argument
  68.         sd.Sort(cmp);
  69.  
  70.         Console.WriteLine("\nAfter Sort: \n");
  71.  
  72.         sd.PrintNames();
  73.     }
  74.  
  75.     // observe  the delegate parameter
  76.     public void Sort(Comparer compare)
  77.     {
  78.         object temp;
  79.  
  80.         for (int i=0; i < names.Length; i++)
  81.         {
  82.             for (int j=i; j < names.Length; j++)
  83.             {
  84.                 // using delegate "compare" just like
  85.                 // a normal method
  86.                 if ( compare(names[i], names[j]) > 0 )
  87.                 {
  88.                     temp = names[i];
  89.                     names[i] = names[j];
  90.                     names[j] = (Name)temp;
  91.                 }
  92.             }
  93.         }
  94.     }
  95.  
  96.     public void PrintNames()
  97.     {
  98.         Console.WriteLine("Names: \n");
  99.  
  100.         foreach (Name name in names)
  101.         {
  102.             Console.WriteLine(name.ToString());
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment