![]() |
Languages »
C# »
Delegates and Events
Intermediate
Event Delegates in Simple English (Really, it's that simple)By Daniel Ang Chee MengA simple tutorial for beginners on the daunting Event Delegates (C#). |
C#.NET1.0, .NET1.1, Win2K, WinXP, Win2003VS.NET2003, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

This article is meant to be a step by step guide to give its reader an understanding of Event Delegates. It's a bit daunting for newbies to .NET. But I'll try my best to break it down to beginners here...
In short, event delegates aren't hard to understand... maybe the thing that perhaps makes it hard to understand is that it's not very straightforward.
Basically, we have to understand 3 things - Yes! Only 3:
EventPublisher). It will have a delegate and event declaration like so: //We declare the event itself here
public event EventDelegate myEvent;
//Our delegation here (in simple english), is to
//publish our event. It sort of says "hey everyone, we have an
//event here that you can subscribe to"...
public delegate void EventDelegate(object from, EventArgs args);
EventSubscriber (you can have many of this), that wants to respond to must implement a function to handle that event. It will add a reference (or connect, in simple English) to one of its methods to the EventPublisher's class event delegate. //Here (in simple English), we (this class) want to subscribe to this
//event delegate (EventDelegate) of EventPublisher. So if the EventPublisher
//raises any event, please send the event to our method this_OnProgress...
theClass3.myEvent += new EventPublisher.EventDelegate(this_OnProgress);
The subscriber will have a method to handle the event:
private void this_OnProgress( object sender, EventArgs e)
{
//Your event handler code goes here
//We could play the infamous "mail arrived" sound here
Console.WriteLine("Event handled in this_OnProgress of EventSubscriber");
//Our sender object gives us some info...
//In your real app, you would read the EventArgs, for example
Console.WriteLine("Sender:" + sender.ToString());
}
EventPublisher class wants to raise an event, it simply calls the delegate. Now, what happens at this point is that, all the subscribers will then be asked to handle the event. //This is where we raise the event from the EventPublisher class
public void issueEvent(EventArgs args)
{
if (myEvent!= null)
{
myEvent(this, args);
}
}
There's a hidden last step: after you're done, simply unsubscribe the event:
~EventSubscriber()
{
theClass3.myEvent -= new EventPublisher.EventDelegate(this_OnProgress);
}
This guy down here is the guy who 'advertises' the event.
using System;
/// <summary>
/// Summary description for EventPublisher.
/// </summary>
///
public class EventPublisher
{
//We declare the event itself here
public event EventDelegate myEvent;
//Our delegation here (in simple english), is to
//publish our event. It sort of says "hey everyone, we have an
//event here that you can subscribe to"...
public delegate void EventDelegate(object from, EventArgs args);
//This is where we raise the event from the EventPublisher class
public void issueEvent(EventArgs args)
{
myEvent(this, args);
}
//We don't need any logic for the constructor, to keep it simple
public EventPublisher()
{
}
//This is the event trigger
//The reason I seperated this is because I wanted to show you
//that we can pass EventArgs to issueEvent
//Usually we will inherit from EventArgs and create our own
//data structure to send information pertaining to the event
public void SendTheEvent()
{
Console.WriteLine("We fire the event here: SendTheEvent()");
this.issueEvent(new EventArgs());
}
}
using System;
/// <summary>
/// Summary description for EventSubscriber.
/// </summary>
public class EventSubscriber
{
public EventSubscriber()
{
//We perform the event subscription here in the Start()..
this.Start();
}
static void Main()
{
//Create an instance of this class
EventSubscriber theClass = new EventSubscriber();
}
private void Start()
{
//We create an instance of the EventPublisher
EventPublisher theClass3 = new EventPublisher();
//Here (in simple English), we (this class) want to subscribe to this
//event delegate (EventDelegate) of EventPublisher. So if the EventPublisher
//raises any event, please send the event to our method this_OnProgress...
theClass3.myEvent += new EventPublisher.EventDelegate(this_OnProgress);
//Here is where the event fires...
//Lets say u're making an email notification program...
//So when an email arrives, this (or some other class, in real life),
//calls the EventPublisher's SendTheEvent method
theClass3.SendTheEvent();
}
private void this_OnProgress( object sender, EventArgs e)
{
//Your event handler code goes here
//We could play the infamous "mail arrived" sound here
Console.WriteLine("Event handled in this_OnProgress of EventSubscriber");
//Our sender object gives us some info...
//In your real app, you would read the EventArgs, for example
Console.WriteLine("Sender:" + sender.ToString());
}
}
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 24 Jun 2003 Editor: Smitha Vijayan |
Copyright 2003 by Daniel Ang Chee Meng Everything else Copyright © CodeProject, 1999-2010 Web18 | Advertise on the Code Project |