6 Important Uses of Delegates and Events

19
6 important uses of Delegates and Events Introduction Abstraction problems of methods and functions How to declare a delegate? Solving the abstract pointer problem using delegates Multicast delegates Simple demonstration of multicast delegates Problem with multicast delegates – naked exposure Events – Encapsulation on delegates Implementing events Difference between delegates and events Delegates for Asynchronous method calls Summarizing Use of delegates Difference between delegates and C++ pointer Source code Introduction In this article we will first try to understand what problem delegate solves, we will then create a simple delegate and try to solve the problem. Next we will try to understand the concept of multicast delegates and how events help to encapsulate delegates. Finally we understand the difference between events and delegates and also understand how to do invoke delegates asynchronously. Once we are done with all fundamentals we will summarize the six important uses of delegates. This is a small Ebook for all my .NET friends which covers topics like WCF,WPF,WWF,Ajax,Core .NET,SQL etc you can download the same from http://tinyurl.com/4nvp9t or else you can catch me on my daily free training @ http://tinyurl.com/39m4ovr Abstraction problems of methods and functions Before we move ahead and we talk about delegates let's try to understand what problem does delegate solve. Below is a simple class named 'ClsMaths' which has a simple 'Add' function. This class 'ClsMaths' is consumed by a simple UI client. Now let's say over a period of time you add subtraction functionality to the 'ClsMaths' class, your client need to change accordingly to accommodate the new functionality. In other words addition of new functionality in the class leads to recompiling of your UI client.

Transcript of 6 Important Uses of Delegates and Events

Page 1: 6 Important Uses of Delegates and Events

6 important uses of Delegates and Events

IntroductionAbstraction problems of methods and functionsHow to declare a delegate?Solving the abstract pointer problem using delegates Multicast delegatesSimple demonstration of multicast delegatesProblem with multicast delegates – naked exposureEvents – Encapsulation on delegatesImplementing eventsDifference between delegates and eventsDelegates for Asynchronous method callsSummarizing Use of delegatesDifference between delegates and C++ pointerSource code   

Introduction 

In this article we will first try to understand what problem delegate solves, we will then create a simple delegate and try to solve the problem. Next we will try to understand the concept of multicast delegates and how events help to encapsulate delegates. Finally we understand the difference between events and delegates and also understand how to do invoke delegates asynchronously.

Once we are done with all fundamentals we will summarize the six important uses of delegates.

This is a small Ebook for all my .NET friends which covers topics like WCF,WPF,WWF,Ajax,Core .NET,SQL etc you can download the same from http://tinyurl.com/4nvp9t  or else you can catch me on my daily free training @ http://tinyurl.com/39m4ovr     

Abstraction problems of methods and functions 

Before we move ahead and we talk about delegates let's try to understand what problem does delegate solve. Below is a simple class named 'ClsMaths' which has a simple 'Add' function. This class 'ClsMaths' is consumed by a simple UI client. Now let's say over a period of time you add subtraction functionality to the 'ClsMaths' class, your client need to change accordingly to accommodate the new functionality. In other words addition of new functionality in the class leads to recompiling of your UI client.  

Page 2: 6 Important Uses of Delegates and Events

In short the problem is that there is a tight coupling of function names with the UI client. So how can we solve this problem?. Rather than referring to the actual methods in the UI / client if we can refer an abstract pointer which in turn refers to the methods then we can decouple the functions from UI. Later any change in the class 'ClsMath' will not affect the UI as the changes will be decoupled by the Abstract pointer. This abstract pointer can be defined by using delegates. Delegates define a simple abstract pointer to the function / method. 

Ok, now that we have understood the tight coupling problem and also the solution, let's try to understand how we can define a simple delegate. 

How to declare a delegate? 

To implement a delegate is a four step process declare, create, point and invoke. 

Page 3: 6 Important Uses of Delegates and Events

The first step is to declare the delegate with the same return type and input parameters. For instance the below function 'add' has two input integer parameters and one integer output parameter.private int Add(int i,int y){return i + y;}

 

So for the above method the delegate needs to be defined in the follow manner below. Please see the delegate keyword attached with the code.  // Declare delegatepublic delegate int PointetoAddFunction(int i,int y);

  The return type and input type of the delegate needs to be compatible, in case they are not compatible it will show the below error as shown in the image below. 

  The next step (second step) is to create a delegate reference. 

Page 4: 6 Important Uses of Delegates and Events

// Create delegate referencePointetoAddFunction myptr = null;

The third step is to point the delegate reference to the method, currently we need to point the delegate reference to the add function.  // Point the reference to the add methodmyptr = this.Add;

  Finally we need to invoke the delegate function using the "Invoke" function of the delegate. // Invoke the delegatemyptr.Invoke(20, 10)

Below figure sums up how the above four step are map with the code snippet. 

Solving the abstract pointer problem using delegates  

In order to decouple the algorithm changes we can expose all the below arithmetic function through an abstract delegate. 

Page 5: 6 Important Uses of Delegates and Events

So the first step is to add a generic delegate which gives one output and takes two inputs as shown in the below code snippet.public class clsMaths{public delegate int PointerMaths(int i, int y);}

  The next step is to expose a function which takes in operation and exposes an attached delegate to the UI as shown in the below code snippet. public class clsMaths{public delegate int PointerMaths(int i, int y);

public PointerMaths getPointer(int intoperation){PointerMaths objpointer = null;if (intoperation == 1){objpointer = Add;}else if (intoperation == 2){objpointer = Sub;}else if (intoperation == 3){objpointer = Multi;}else if (intoperation == 4)

Page 6: 6 Important Uses of Delegates and Events

{objpointer = Div;}return objpointer;}}

  Below is how the complete code snippet looks like. All the algorithm functions i.e. 'Add' , 'Sub' etc are made private and only one generic abstract delegate pointer is exposed which can be used to invoke these algorithm function. public class clsMaths{public delegate int PointerMaths(int i, int y);

public PointerMaths getPointer(int intoperation){PointerMaths objpointer = null;if (intoperation == 1){objpointer = Add;}else if (intoperation == 2){objpointer = Sub;}else if (intoperation == 3){objpointer = Multi;}else if (intoperation == 4){objpointer = Div;}return objpointer;}

private int Add(int i, int y){return i + y;}private int Sub(int i, int y){return i - y;}private int Multi(int i, int y){return i * y;}private int Div(int i, int y){return i / y;}}

  So at the client side the calls becomes generic without any coupling with the actual method names like 'Add' , 'Sub' etc. int intResult = objMath.getPointer(intOPeration).Invoke(intNumber1,intNumber2);

 

Multicast delegates 

In our previous example we have see how we can create a delegate pointer to a function or method. We can also create a delegate

Page 7: 6 Important Uses of Delegates and Events

which can point to multiple functions and methods. If we invoke such delegate it will invoke all the methods and functions associated with the same one after another sequentially.Below is the code snippet which attaches 2 methods i.e. method1 and method2 with delegate 'delegateptr'. In order to add multiple methods and function we need to use '+=' symbols. Now if we invoke the delegate it will invoke 'Method1' first and then 'Method2'. Invocation happen in the same sequence as the attachment is done. // Associate method1delegateptr += Method1;// Associate Method2delegateptr += Method2;// Invoke the Method1 and Method2 sequentiallydelegateptr.Invoke();

  So how we can use multicast delegate in actual projects. Many times we want to create publisher / subscriber kind of model. For instance in an application we can have various error logging routine and as soon as error happens in a application you would like to broadcast the errors to the respective components. 

Simple demonstration of multicast delegates 

In order to understand multicast delegates better let's do the below demo. In this demo we have 'Form1', 'Form2' and 'Form3'. 'Form1' has a multicast delegate which will propagate event to 'Form2' and 'Form3'. 

At the form level of 'Form1' (this form will be propagating events to form2 and form3) we will first define a simple delegate and reference of the delegate as shown in the code snippet below. This delegate will be responsible for broadcasting events to the other

Page 8: 6 Important Uses of Delegates and Events

forms. // Create a simple delegatepublic delegate void CallEveryOne();

// Create a reference to the delegatepublic CallEveryOne ptrcall=null;// Create objects of both forms

public Form2 obj= new Form2();public Form3 obj1= new Form3();

  In the form load we invoke the forms and attach 'CallMe' method which is present in both the forms in a multicast fashion ( += ). private void Form1_Load(object sender, EventArgs e){// Show both the formsobj.Show();obj1.Show();// Attach the form methods where you will make call backptrcall += obj.CallMe;ptrcall += obj1.CallMe;}

  Finally we can invoke and broadcast method calls to both the forms. private void button1_Click(object sender, EventArgs e){// Invoke the delegateptrcall.Invoke();}

 

Problem with multicast delegates – naked exposure 

The first problem with above code is that the subscribers (form2 and form3) do not have the rights to say that they are interested or not interested in the events. It's all decided by 'form1'. We can go other route i.e. pass the delegate to the subscribers and let them attach their methods if they wish to subscribe to the broadcast sent by 'form1'. But that leads to a different set of problems i.e. encapsulation violation. If we expose the delegate to the subscriber he can invoke delegate, add his own functions etc. In other words the delegate is completely naked to the subscriber. 

Events – Encapsulation on delegates 

Events help to solve the delegate encapsulation problem. Events sit on top of delegates and provide encapsulation so that the

Page 9: 6 Important Uses of Delegates and Events

destination source can only listen and not have full control of the delegate object.Below figure shows how the things look like:-• Method and functions are abstracted /encapsulated using delegates• Delegates are further extended to provide broadcasting model by using multicast delegate.• Multicast delegate are further encapsulated using events. 

Implementing events 

So let's take the same example which we did using multicast delegates and try to implement the same using events. Event uses delegate internally as event provides higher level of encapsulation over delegates. So the first step in the publisher ('Form1') we need to define the delegate and the event for the delegate. Below is the code snippet for the same and please do notice the 'event' keyword.We have defined a delegate 'CallEveryOne' and we have specified an event object for the delegate called as 'EventCallEveryOne'. public delegate void CallEveryone();public event CallEveryone EventCallEveryOne;

  From the publisher i.e. 'Form1' create 'Form2' and 'Form3' objects and attach the current 'Form1' object so that 'Form2' and 'Form3' will listen to the events. Once the object is attached raise the events. Form2 obj = new Form2();obj.obj = this;Form3 obj1 = new Form3();obj1.obj = this;obj.Show();obj1.Show();EventCallEveryOne();

  At the subscriber side i.e. (Form2 and Form3) attach the method to the event listener.  obj.EventCallEveryOne += Callme;

  This code will show the same results as we have got from multicast delegate example. 

Difference between delegates and events 

Page 10: 6 Important Uses of Delegates and Events

So what's really the difference between delegates and events other than the sugar coated syntax of events. As already demonstrated previously the main difference is that event provides one more level of encapsulation over delegates.So when we pass delegates it's naked and the destination / subscriber can modify the delegate. When we use events the destination can only listen to it. 

Delegates for Asynchronous method calls 

One of the other uses of delegates is asynchronous method calls. You can call methods and functions pointed by delegate asynchronously. Asynchronous calling means the client calls the delegate and the control is returned back immediately for further execution. The delegate runs in parallel to the main caller. When the delegate has finished doing his work he makes a call back to the caller intimating that the function / subroutine has completed executing. 

Page 11: 6 Important Uses of Delegates and Events

To invoke a delegate asynchronously we need call the 'begininvoke' method. In the 'begininvoke' method we need to specify the call back method which is 'CallbackMethod' currently. delegateptr.BeginInvoke(new AsyncCallback(CallbackMethod), delegateptr);

  Below is the code snippet for 'CallbackMethod'. This method will be called once the delegate finishes his task. static void CallbackMethod(IAsyncResult result){int returnValue = flusher.EndInvoke(result);}

 

Summarizing Use of delegates 

There are 6 important uses of delegates:- 1. Abstract and encapsulate a method (Anonymous invocation)

This is the most important use of delegates; it helps us to define an abstract pointer which can point to methods and functions. The same abstract delegate can be later used to point to that type of functions and methods. In the previous section we have shown a simple example of a maths class. Later addition of new algorithm functions does not affect the UI code.

2. Callback mechanismMany times we would like to provide a call back mechanism. Delegates can be passed to the destination and destination can use the same delegate pointer to make callbacks.

3. Asynchronous processingBy using 'BeginInvoke' and 'EndInvoke' we can call delegates asynchronously. In our previous section we have explained the same in detail.

4. Multicasting - Sequential processing Some time we would like to call some methods in a sequential manner which can be done by using multicast delegate. This is already explained in the multicast example shown above.

5. Events - Publisher subscriber modelWe can use events to create a pure publisher / subscriber model. 

Difference between delegates and C++ pointer 

Page 12: 6 Important Uses of Delegates and Events

C++ pointers are not type safe, in other words it can point to any type of method. On the other hand delegates are type safe. A delegate which is point to a return type of int cannot point to a return type of string. 

Page 13: 6 Important Uses of Delegates and Events

Display Your Form as Modal Dialog and Modeless Dialog

Introduction  In this article I will walk you through how to create different forms and access form from other forms. While we are developing I will help you to understand modal and modeless forms and some important control properties.   We will create three forms. One is a Main form that will invoke and display an About form and an Address Form. The About form is invoked as Modeless dialog and the Address Form is invoked as a Modal dialog.   Let us start the step by step.   Note: If you are experienced, and do want to slide fast: 

1)      Open the attached project 2)      Look at the form properties in bold as well refer the brown texts here 3)     Search the project with two tags: _Mode_less , _Model [ To follow the code easily at a glance]

  Main Form  First we will design the Main form that invokes two other forms. Follow the steps below:   1. Open visual studio 2005 and create a new Visual C# Windows Application. 2. Use the following picture as the reference for creating the Form  

I marked all the controls with some numbers to ask you to make some changes for the corresponding controls.  1: TextBox Control. Actually no property change is required for this text box. When I am creating the picture I forgot it and kept a number for it. So leave it as default 2: Button Control. Change the Name property to btnGetAddress 4: Button Control. Change the Name property to btnAbout 3: TextBox Control. Change the Name property to txtAddress. Set MultiLine to true. This will allow you resize the text box to have different height. When MultiLine property is false the resize in terms of Height is not allowed. Set the ReadOnly to true. When set to true this property does not allow the control to be editable by the end user. But, you can edit it through the code at runtime. Change the BackColor property to White as the ReadOnly property changed it to some Gray color.   If you wish you can set the Background and foreground colors as you like.   The About Box (Form)  This is the second form that we are going to add to our project. When you created the Windows Application, the wizard created a main form for us and that for us is the Start Up form.    Suppose if you want to change the Startup form, what will you do?  

Page 14: 6 Important Uses of Delegates and Events

Double click the Program.cs file and scroll to find the following piece of code:   static void Main() {     Application.EnableVisualStyles();     Application.SetCompatibleTextRenderingDefault(false);     Application.Run(new frmMain()); }   The Application.Run method takes our Main form Object and displays it. I renamed the form1 to frmMain by right clicking it in the solution explorer and using the Rename menu option.   To add our second form:  1) Goto the solution Explorer using View|Solution Explorer if it is already not available on your right side pane. 2) Select the solution Name, right click on it, and chose Add|New Item  

  3) In the displayed dialog select Windows Form and Change the File Name to frmAbout.cs  

  4) Place a Label control on the Form. Set the Text property as shown in the Picture below. Note, you need to use the Down Arrow button of the text property value to type multiple lines for the label. The screen shot is shown below:  

 

  The Address Dialog Box  Now we will add a new form and display it like Modal dialog. Follow the Steps below and I will tell you what is Modal dialog and Modeless dialog when you run the finished application.   1) Add a new Windows form as you did in the previously. Name the file as AddressDlg.cs 2) Place 5 Labels and 5 Text boxes to the Form. Add two button controls 3) Arrange the Form as shown below:  

Page 15: 6 Important Uses of Delegates and Events

1: Set the Name property to txtDoorNumber 2: Set the Name property to txtStreet 3: Set the Name property to txtArea 4: Set the Name property to txtCity 5: Set the Name property to txtPin 7: Set the Name property to btnOK. Set the text property to OK. And select OK as the DialogResult Property. 8: Set the Name property to btnCancel. Set the text property to OK. And select Cancel as the DialogResult Property. 9: Set the Name property to AddressDlg. Select btnOK for the AcceptButton property. Select btnCancel for the CancelButton property.   We are done with designing the Address form. Now let me explain about the above form. As I already said, we are going to display the above form as modal. What is the difference between modal and modeless dialogs (or forms)?   When we display a form as Modeless, you can still access the Main form that called the Modeless dialog. But, the access to the Main form is prohibited when we display the same dialog as Modal. Let us say the Instance of the above dialog as X, X.Show() will display the dialog as modeless where as X.ShowDialog() will display the dialog as Modal. If you remember, in this Article we are going to display the About form as Modeless and Address form as Modal.   We set btnOK as AcceptButton and btnCancel as the Cancelbutton properties for the form. This is to hook-up the Esc and Enter keystrokes on the form to the OK and Cancel button clicks. So hitting enter button on the form runs the OK button event handler. Hitting Esc will run the cancel button event handler.   The ShowDialog() function will return some constants based on how the form is closed (actually hidden) by the interacting user. We can set these constants to the button on the form. We set DialogResult property for the button btnOK as OK, and for the button btnCancel as Cancel. For example if the user closes the dialog by clicking the btnOK the constant OK is passed back to the caller.   Hope all the designs are done now. If you need you can open the attached project for reference. Open the forms and look at the properties that appear in bold.   Button Handler on Main form  First let us display the About Form as Modeless dialog.   1) Open the Main form in the designer. 2) Double click the button About. 3) Now add the following code in the handler you are in:   //001_Mode_less: Display the Modeless Dialog frmAbout AboutForm = new frmAbout(); AboutForm.Show();   In the above code first we created the object of frmAbout and the object name is AboutForm. Then we are calling AboutForm.Show() to display the about form as modeless dialog. Also note the calling

Page 16: 6 Important Uses of Delegates and Events

code is not blocked and it continues executing the next statement after the call to the Show method. That means User Interaction is allowed on both About Form as well as the Main Form that invoked it.   4) Now, double click the … button 5) In the Handler write the following code [Please try writing in stead of copy paste. The pop-ups on the code window give you some Nice Exp   //004_Model: Invoke the Model Dialog and set the Readonly Address //Text box AddressDlg dlg = new AddressDlg(); if (dlg.ShowDialog() == DialogResult.OK)     txtAddress.Text  = dlg.Address; else     txtAddress.Text = "---";     After creating the instance (dlg) of AddressDlg we are displaying the dialog as a modal dialog. This we use the method ShowDialog() and the call to this method block the calling code until the dialog is closed. The return value of function (the value you set for the button's DialogResult property) is checked to make sure OK button is clicked by the user. In that case (OK button click) we take the Address property from dialog and display it in the Multi Line text box Address.   Note that all the controls in the Form are Private. If you need you can change it by setting the Modifier property of the control, which is not advisable. I do expose a property Address, which stores the values, entered by the user in the text fields.   Coding the Address Form (OK. Modal Dialog)  1) Right click and select view code on the Address Form 2) Declare a private string that stores the address entered in the text boxes   //002_Model: Declare a Private Field private string mAddress;   3) Next add a public property that give access to the Address field   //003_Model: Read Only Property public string Address {     get { return mAddress; } }   4) Go to the design view of the address 5) Double click the button OK 6) In the displayed event handler write the following code   //001_Model: Set the Address Member variable based on the User Input mAddress = txtDoorNumber.Text + ";" + txtStreet.Text + Environment.NewLine +     txtArea.Text + Environment.NewLine + txtCity.Text + Environment.NewLine +     "Pin -" + txtPin.Text;   We store all the Address parts in the local private field by combining all the text values entered in the form. And, we do this on the handler for OK button click, as we know all the texts are ready from user perspective. The Address property is public and gives access to the Field mAddress for external world.   Run the Project  1) Click the Run button 2) Click the About Button   Now you can move the both Main for about forms. Which means the caller Main form is not blocked and responds to your User input Click & Drag (Move).   3) Close the about form (If you need) 4) Click the Ellipsis button  

Page 17: 6 Important Uses of Delegates and Events

The displayed address form blocks the caller from the main form. When the address form is closed, the main form can again receive user input.   5) Type in the Address Parts and Close the Dialog. [ You can try Esc and Hitting Enter and Clicking cancel. ]