Friday, November 29, 2013

C# Event – Understanding C# events – Part 3


Before going further it is really important to go through the Part 1 and Part 2.
AS discussed in Part 2, I will create an application based on Events. So create BallEventArgs  class as shown below –
public class BallEventArgs : EventArgs
    {
        public int Height { get; set; }
        public int Length { get; set; } 

        public BallEventArgs(int h, int l)
        {
            Height = h;
            Length = l;
        }
    }

Create Ball class as hown below with event declaration –
public class Ball
    {
        //declare event BallInGround
        public event EventHandler BallInGround; 

        /// <summary>
        /// method used for raising the event BallInGround
        /// </summary>
        /// <param name="e">BallEventArgs object</param>
        public void OnBallInGround(BallEventArgs e)
        {
            if (BallInGround != null)
            {
                BallInGround(this, e);
            }
        }
    }

Add a Fielder class as shown below –

public class Fielder
    {       
        public Fielder(Ball ball)
        {
            //register event
            ball.BallInGround += new EventHandler(ball_BallInGround);
        }

        //event handler method
        void ball_BallInGround(object sender, EventArgs e)
        {
            if (e is BallEventArgs)
            {
                BallEventArgs ballEventArgs = e as BallEventArgs;
                if (ballEventArgs.Height < 60)
                {
                    Console.WriteLine("Fielder: caught the ball!!");
                }
                else if (ballEventArgs.Height > 90 && ballEventArgs.Length > 90)
                {
                    Console.WriteLine("Fielder: It's a Six!!");
                }
                else
                {
                    Console.WriteLine("Fielder: field the ball!!");
                }
            }
        }
    }
 
Fan class is as follows –
public class Fan
    {
        public Fan(Ball ball)
        {
            ball.BallInGround += new EventHandler(ball_BallInGround);
        }
        void ball_BallInGround(object sender, EventArgs e)
        {
            if (e is BallEventArgs)
            {
                BallEventArgs ballEventArgs = e as BallEventArgs;
                if (ballEventArgs.Height < 60)
                {
                    Console.WriteLine("Fan: ohh nooo..it's a wicket down!!");
                }
                else if (ballEventArgs.Height > 90 && ballEventArgs.Length > 90)
                {
                    Console.WriteLine("Fan: yoo hoooo it's a six!!");
                }
                else
                {
                    Console.WriteLine("Fan: boring...!!");
                }
                Console.ReadLine();
            }
        }
    }

Form code behind class wil be as follows –
public partial class Form1 : Form
    {
        private Ball ball = null;
        Fan fan = null;
        Fielder fielder = null;

        public Form1()
        {
            InitializeComponent();           
            ball = new Ball();
            fielder = new Fielder(ball);
            fan = new Fan(ball);           
        }

        private void btnHit_Click(object sender, EventArgs e)
        {
            BallEventArgs ballEventArgs = new BallEventArgs(Convert.ToInt16(txtHeight.Text), Convert.ToInt16(txtLength.Text));
            ball.OnBallInGround(ballEventArgs);
        }
    }

Following are the replies received on different inputs in Output window –
 


 
 



This is how you can use Events in c#. Hope this application has given you basic implementation details related to C# Event. Now let’s discuss few real life implementations of C# events and concepts related to C# Events in Part 4(In Progress).

No comments:

Post a Comment