Events & Delegates

Delegate is a type, which signifies the signature of any function. Delegate object is a function pointer kind. Whereas Event can be considered as object in memory with type of some delegate which can hold addresses of several functions. So a delegate object & event are somewhat similar things in some lines.

Let’s try to dig Events & Delegates through some situation.IE..I have a parent form with one label and one button in it. On
Clicking that button, need to open Child Modal dialogue which has in turn has only one button. On Clicking that Child Form button, need to change the text of parent form label.
I’ll achieve the required functionality through both events and delegate objects.
First, the Parent Form looks like..

While clicking the ‘OpenChildForm’ button, the following child
form opens..so the requirement is While clicking the ‘ChangeTextInParentLabel’ the label1 text of parent form need to change.


The corresponding the ParentForm.cs code is
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EventHandling
{
public partial class ParentForm Form
{
public ParentForm()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
ChildForm1 childObj = new ChildForm1();
//Event Subscription
childObj.ChildButtonClick+=new ChildForm1.ChildCustomDelegate(childObj_ChildButtonClick);
childObj.ShowDialog();
}
private void childObj_ChildButtonClick(string message)
{
label1.Text = message;
}
}
}

And the corresponding code for 
ChildForm1.cs is

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EventHandling
{
public partial class ChildForm1 Form
{
public delegate void ChildCustomDelegate(string message);
public event ChildCustomDelegate ChildButtonClick;
private void EventRaiser(string message)
{
ChildButtonClick(message);
}
public ChildForm1()
{
InitializeComponent();
}
private void ChangeTextInParentLabel_Click(object sender, EventArgs e)
{
//Supppose my text to appear in the parent form is “Happy New Year”
//Event Raising
EventRaiser(“Happy New Year”);
}
}
}

Same Implementation Using Delegate objects…
Parent.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EventHandling
{
public partial class ParentForm Form
{
public ParentForm()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
ChildForm1 childObj = new ChildForm1();
//Event Subscription
childObj.MyDelegateObject+=new ChildForm1.ChildCustomDelegate(childObj_ChildButtonClick);
childObj.ShowDialog();
}
private void childObj_ChildButtonClick(string message)
{
label1.Text = message;
}
}
}

And ChildForm1.cs is like..
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EventHandling
{
public partial class ChildForm1 : Form
{
public delegate void ChildCustomDelegate(string message);
public ChildCustomDelegate MyDelegateObject;
private void DelegateRaiser(string message)
{
MyDelegateObject(message);
}
public ChildForm1()
{
InitializeComponent();
}
private void ChangeTextInParentLabel_Click(object sender, EventArgs e)
{
//Supppose my text to appear in the parent form is “Happy New Year”
//Delagate Raising
DelegateRaiser(“Happy New Year”);
}
}
}


But when to use Events and when to go for delegates objects..???
Here is a lay down of differences between events & delegate objects.
  1. We can achieve the functionality of events through delegate objects, but outside the class where they are declared, delegate objects can also be invoked. Whereas events cannot be raised outside its class. Events can only either be subscribed or unsubscripted.
  2. Delegate is a type, so can be a namespace member. whereas event need to be class member

Advertisements