Delegates ed eventi in pratica.

   1: /*
   2:  * Creato da SharpDevelop.
   3:  * Utente: George
   4:  * Data: 12/05/2008
   5:  * Ora: 18.03
   6:  * 
   7:  */
   8:  
   9: using System;
  10: using System.IO;
  11: using System.Collections.Generic;
  12: using System.Text;
  13: using System.Timers; 
  14:  
  15: namespace Sample.Delegates
  16: {
  17:     
  18:     public class Person
  19:     {
  20:         private string name;
  21:         
  22:         public string Name {
  23:             get { return name; }
  24:             set { name = value; }
  25:         }
  26:         private int age;
  27:         
  28:         public int Age {
  29:             get { return age; }
  30:             set { age = value; }
  31:         }
  32:         private FootBallTeam supporterOf;
  33:         
  34:         public FootBallTeam  SupporterOf {
  35:             get { return supporterOf; }
  36:             set { supporterOf = value; }
  37:         }
  38:                 
  39:         public Person(string name, int age,FootBallTeam supporterOf)
  40:         {
  41:             this.name = name;
  42:             this.age = age;
  43:             this.supporterOf = supporterOf;
  44:         }
  45:         
  46:         public void CheckWinnerTeam(object sender, EventArgs e)
  47:         {
  48:             
  49:             List<FootBallTeam> teams = ((InizializeSample)sender).footBallTeams;
  50:             teams.Sort(delegate(FootBallTeam f1,FootBallTeam f2){return f1.Position.CompareTo(f2.Position);});
  51:             foreach(FootBallTeam t in teams)
  52:             {
  53:                 Console.WriteLine(string.Format("Team : {0} - position : {1})",t.TeamName,t.Position.ToString()));
  54:             }
  55:             if(this.SupporterOf.TeamName == teams[0].TeamName)
  56:             {
  57:                 Console.WriteLine("Mario's team ({0}) placed 1st, Mario is happy!",this.SupporterOf.TeamName);
  58:             }
  59:             else
  60:             {
  61:                 Console.WriteLine(string.Format("Marios'team ({0}) placed {1}, Mario is sad :(",this.SupporterOf.TeamName, this.SupporterOf.Position));
  62:             }
  63:             
  64:         }
  65:         
  66:     }
  67:     
  68:     public class FootBallTeam
  69:     {
  70:         
  71:         private string teamName;
  72:         
  73:         public string TeamName {
  74:             get { return teamName; }
  75:             set { teamName = value; }
  76:         }
  77:         private int position;        
  78:         
  79:         public int Position {
  80:             get { return position; }
  81:             set { position = value; }
  82:         }
  83:     
  84:         public FootBallTeam(string teamName)
  85:         {
  86:             this.teamName = teamName;    
  87:         }
  88:         
  89:         public void PlayChampionship(object sender, EventArgs e)
  90:         {
  91:             List<FootBallTeam> footballTeams = ((InizializeSample)sender).footBallTeams;    
  92:             int randomPosition;
  93:             do
  94:             {
  95:                 int seed = DateTime.Now.Millisecond;
  96:                 Random randNum = new Random(seed);
  97:                 int a = randNum.Next(1,6);
  98:                 randomPosition = a;
  99:             }    
 100:             while(footballTeams.Exists(delegate(FootBallTeam f){return f.Position == randomPosition;}));                    
 101:             this.position = randomPosition;
 102:         }        
 103:         
 104:     }    
 105:     
 106:     
 107:     public class InizializeSample
 108:     {
 109:         public List<Person> persons = new List<Person>();
 110:         public List<FootBallTeam> footBallTeams = new List<FootBallTeam>();
 111:         public delegate void SampleDelegateEventHandler(object sender, EventArgs e);
 112:                 
 113:         public event SampleDelegateEventHandler sampleDelegate;
 114:                 
 115:         public InizializeSample()
 116:         {
 117:                     
 118:             footBallTeams.Add(new FootBallTeam("Roma"));
 119:             footBallTeams.Add(new FootBallTeam("Lazio"));
 120:             footBallTeams.Add(new FootBallTeam("Milan"));
 121:             footBallTeams.Add(new FootBallTeam("Juventus"));
 122:             footBallTeams.Add(new FootBallTeam("Inter"));    
 123:             
 124:             foreach(FootBallTeam footBallTeam in footBallTeams)
 125:             {
 126:                 sampleDelegate+= new SampleDelegateEventHandler(footBallTeam.PlayChampionship);                
 127:             }    
 128:             
 129:             persons.Add(new Person("Mario",24, footBallTeams.Find(delegate(FootBallTeam f){return f.TeamName == "Roma";})));            
 130:             sampleDelegate+= new SampleDelegateEventHandler((persons.Find(delegate(Person p){return p.Name=="Mario";})).CheckWinnerTeam);
 131:             //Invoking the delegate
 132:             sampleDelegate(this,new EventArgs());
 133:         }
 134:     
 135:     }
 136:     
 137:     public class EntryPoint
 138:     {
 139:         public static void Main()
 140:         {
 141:                 Console.WriteLine("Campionato");            
 142:                 InizializeSample inizialize = new InizializeSample();
 143:                 Console.ReadKey(true);
 144:         }
 145:     }
 146:     
 147: }

Advertisements