Thursday, June 19, 2014

Events and Delegates

This is the best code snippet to make clean understanding about the usage of Events and Delegates.

class Program  
   {     
     static void Main(string[] args)  
     {  
       Ball ball = new Ball();  
       BallEventArgs balEvt = new BallEventArgs(10, 10);  
       Pitcher pi = new Pitcher(ball);  
       ball.OnBallInPlay(balEvt);  
     }       
   }  

 Delegate:

 public class Ball {  
     public delegate void eventDelegate(object o,EventArgs e);  
     public event eventDelegate BallInPlay;  
     public void OnBallInPlay(BallEventArgs e)  
     {  
       eventDelegate ballInPlay = BallInPlay;  
       if (ballInPlay != null)  
         ballInPlay(this, e);  
     }  
   }  
   public class BallEventArgs:EventArgs{  
     public int Distance { get; private set; }  
     public int Trajectory { get; private set; }  
     public BallEventArgs(int trajectory, int distance)  
     {  
       this.Trajectory = trajectory;  
       this.Distance = distance;  
     }  
   }  
   class Pitcher  
   {  
     public Pitcher(Ball ball)  
     {  
       ball.BallInPlay += new Ball.eventDelegate(ball_BallInPlay);  
     }  
     void ball_BallInPlay(object sender, EventArgs e)  
     {  
       if (e is BallEventArgs)  
       {  
         BallEventArgs ballEventArgs = e as BallEventArgs;  
         if ((ballEventArgs.Distance < 95) && (ballEventArgs.Trajectory < 60))  
           CatchBall();  
         else  
           CoverFirstBase();  
       }  
     }  
     private void CatchBall()  
     {  
       Console.WriteLine("Pitcher: I caught the ball");  
     }  
     private void CoverFirstBase()  
     {  
       Console.WriteLine("Pitcher: I covered first base");  
     }  
   }  

  EventHandler: 

public class Ball {  
     public event EventHandler BallInPlay;  
     public void OnBallInPlay(BallEventArgs e)  
     {  
       EventHandler ballInPlay = BallInPlay;  
       if (ballInPlay != null)  
         ballInPlay(this, e);  
     }  
   }  
   public class BallEventArgs:EventArgs{  
     public int Distance { get; private set; }  
     public int Trajectory { get; private set; }  
     public BallEventArgs(int trajectory, int distance)  
     {  
       this.Trajectory = trajectory;  
       this.Distance = distance;  
     }  
   }  
   class Pitcher  
   {  
     public Pitcher(Ball ball)  
     {  
       ball.BallInPlay += new EventHandler(ball_BallInPlay);  
     }  
     void ball_BallInPlay(object sender, EventArgs e)  
     {  
       if (e is BallEventArgs)  
       {  
         BallEventArgs ballEventArgs = e as BallEventArgs;  
         if ((ballEventArgs.Distance < 95) && (ballEventArgs.Trajectory < 60))  
           CatchBall();  
         else  
           CoverFirstBase();  
       }  
     }  
     private void CatchBall()  
     {  
       Console.WriteLine("Pitcher: I caught the ball");  
     }  
     private void CoverFirstBase()  
     {  
       Console.WriteLine("Pitcher: I covered first base");  
     }  
   }  

 

No comments:

Post a Comment

Code Formater

Paste Here Your Source Code
Source Code Formatting Options
1) Convert Tab into Space :
2) Need Line Code Numbering :
3) Remove blank lines :
4) Embeded styles / Stylesheet :
5) Code Block Width :
6) Code Block Height :
7) Alternative Background :
Copy Formatted Source Code
 
Preview Of Formatted Code