Showing posts with label Delegates. Show all posts
Showing posts with label Delegates. Show all posts

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:


C# Notify Parent From Child using events and delegates

While creating an application, we often come across scenarios where we have parent child relationship between objects.

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:

events in C#

An event allows a class or resource to notify the other classes or resource, when something required event occurs. The resource that raises the events is knows as publisher and the resource that handles the event is known as subscriber. In C# visual application we handle events like button clicked, combo box selection changed events etc. Similarly, we can raise our custom events when requires.

Overview of events

  • Publisher determines when to raise event, and subscriber determines how to handle the event.
  • An event can have multiple subscribers, and subscriber can handle multiple events.
  • An event that has no subscriber is never raised.
  • Events are typically used for user actions.
  • When an event has multiple subscriber than event handlers are invoked synchronously.
  •  Events are actually based on the EventHandler delegates.

Example of Events

delegates in C#

A delegate is a type that references a method. Once a delegate is bound to a method, it behaves like method. It can be used like other methods.


public delegate void valueEntered(string value);

Any method that matches the delegate's signature including return type, parameters etc, can be assigned to the delegate.

delegate's Overview

  • It is like a function pointer in C++.
  • It allows methods to be passed as parameters
  • It can be used as callback method.
  • In this method's signature need not to be exactly same as delegate's.
                     For Example:
                                public class type1 { }
                    public class type2 : type1 { }
                    public delegate type1 delegateEx();
                    class Program
                       {
                          static void Main(string[] args)
                           {
                             delegateEx de = new delegateEx(MethodType1);
                             delegateEx de1 = new delegateEx(MethodType2);
                           }
                          public static type1 MethodType1() { return new type1(); }
                          public static type2 MethodType2() { return new type2(); }
                        }
                   
          type1 and type2 are 2 different classes but type2 inherits type1 so It is allowed in delegates.
  • C# 2.0 introduces concept of Anonymous method which allows programmer to pass code block as parameter in method.

delegate's Example