C# Sending Information / data from Child to Parent class using events and delgates

In the previous post C# Notify Parent From Child using events and delegates , we were just notifying the parent class about some processing happening in the child class.

 But we were not sending any information or data to parent class about what processing is happening in child class. We have taken the same example taken in C# Notify Parent From Child using events and delegates and modified a bit so that we can send information to parent about the processing happening in Child class.
Previously, in Parent we were getting information that some key is pressed in Child Class. Now we will send the information that which key is pressed in Child class.

We have modified the delegate to be method with parameters. Now the signature of the event handler will also change. Also we have defined a custom class "CustomEventArgs" which has one public property i.e Key, which has the information about the pressed key.

Now whenever Child class raises an event, it will create and instance of CustomEventArgs and send it to all the event listeners. So all the event listeners, in our case the Parent class, will get the information about which key is pressed.

Source Code:






namespace ParentChildEvents
{
    class Program
    {
        static void Main(string[] args)
        {
            //Creating a parent class
            Parent p = new Parent();
        }
    }

    //Delegate which will used for creating an event in Child Class.
    //While raising an event from child class, CustomEventArgs instance can be instantiated
    //and required data can be send to parent class
    public delegate void NotifyParentDelegate(CustomEventArgs customEventArgs);

    public class Parent
    {
        //Child class object
        private Child _child;
        public Parent()
        {
            //Initializing Child class in Parent Constructor
            _child = new Child();
            //Registering child class event
            _child.NotifyParentEvent += new NotifyParentDelegate(_child_NotifyParentEvent);
            _child.ReadKeyInChild();
        }

       

        //Event Listener. This function will be called whenever child class raises event.
        void _child_NotifyParentEvent(CustomEventArgs customEventArgs)
        {
            System.Console.WriteLine("\n");
            System.Console.WriteLine("Parent : Key Pressed In Child");
            System.Console.WriteLine(customEventArgs.Key + " is pressed in Child Class");
        }
    }

    //Child Class
    public class Child
    {
        public Child()
        {
           
        }
        //Public Event which will be registered by Parent Class
        public event NotifyParentDelegate NotifyParentEvent;

       
        public void ReadKeyInChild()
        {
            bool _continue = true;
            do
            {
                System.Console.WriteLine("\nPlease press key");
                ConsoleKeyInfo pressedKeyInfo =  System.Console.ReadKey();               
               
                NotifyParent(pressedKeyInfo.Key.ToString());

                //System.Console.WriteLine("\nDo you want to notify parent?");
                //ConsoleKeyInfo keyInfo = System.Console.ReadKey();
                //if (keyInfo.Key==ConsoleKey.Y)
                //{
                    //Call to notify parent that key has been pressed
                   
                //}

                System.Console.WriteLine("Do you want to continue?");
                //System.Console.WriteLine("\n");
                ConsoleKeyInfo keyInfo = System.Console.ReadKey();
                if (keyInfo.Key == ConsoleKey.Y)
                {
                    _continue = true;
                }
                else
                {
                    _continue = false;
                }
            } while(_continue);
        }
        public void NotifyParent(string pressedKey)
        {
            if (NotifyParentEvent != null)
            {
                CustomEventArgs customEventArgs = new CustomEventArgs(pressedKey);
                //Raise Event. All the listeners of this event will get a call.
                NotifyParentEvent(customEventArgs);
            }
        }
    }

    public class CustomEventArgs : EventArgs
    {
        public CustomEventArgs(string key)
        {
            _key = key;
        }
        private string _key;

        public string Key
        {
            get { return _key; }
            set { _key = value; }
        }
    }
}

ScreenShot:

Here is the screenshot of the output of the console application:

No comments:

Post a Comment