When we want to pass some information from parent to child, it is very simple as we have the reference of the child object in the parent and we pass the information using message passing technique, where we call some public method of child and achieve the desired result.
But many times while creating applications, we find some requirement where we need to notify the parent class that some processing has happened in child class. Based on that we need to do something in parent class. Notifying parent from child is not direct way as child do not hold reference of parent.
Note : We can always pass parent's reference to child but it is not the conventional way and can cause issues while maintaining the application. So this is not recommended.
So, we can achieve this using delegates. We can create and event in child using delegate. Parent will listen to this event. Whenever some processing happens in child, child can raise event and whoever is listening to this event will get notified that some processing has happened in child.
Sample Code:
We have created a simple console application which shows how we can raise event from child to parent. We have created two class "Parent" and "Child". Parent class objects holds the reference of the Child class.
We are reading a key press in child class and whenever a key is pressed, we are raising an event in child class which is registered by Parent class. So parent is notified that the key is pressed in the Child class.
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 public delegate void NotifyParentDelegate(); 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() { System.Console.WriteLine("\n"); System.Console.WriteLine("Parent : Key Pressed In Child"); } } //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"); System.Console.ReadKey(); 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 NotifyParent(); } System.Console.WriteLine("Do you want to continue?"); keyInfo = System.Console.ReadKey(); if (keyInfo.Key == ConsoleKey.Y) { _continue = true; } else { _continue = false; } }while(_continue); } public void NotifyParent() { if (NotifyParentEvent != null) { //Raise Event. All the listeners of this event will get a call. NotifyParentEvent(); } } }
Screenshot:
Here is the screenshot of the output of the console application.
In this application we are just notifying the parent about some event but not passing some data. We can also pass some information to parent such as which key is pressed or something else depending on our requirement. We will see how information can be passed from child to parent using events in the next section.
No comments:
Post a Comment