Introduction
Check out this simple event delegate code. First, you have a source which generates an event:
using System;
using System.Collections.Generic;
using System.Text;
namespace eventDelegate
{
class Source
{
public event EventHandler SomeEvent;
public void RaiseEvent()
{
if (null != SomeEvent)
{
SomeEvent(this, null);
}
}
}
}
Next, you have a receiver which is supposed to get a notification of the event:
using System;
using System.Collections.Generic;
using System.Text;
namespace eventDelegate
{
class Receiver
{
public void NotifyMe(object source, EventArgs e)
{
Console.WriteLine("I am notified by "+source.GetType());
}
}
}
Then you have the main program which does the wiring:
using System;
using System.Collections.Generic;
using System.Text;
namespace eventDelegate
{
class Program
{
static void Main(string[] args)
{
Source theSource = new Source();
Receiver theReceiver = new Receiver();
theSource.SomeEvent += new EventHandler(theReceiver.NotifyMe);
theSource.RaiseEvent();
}
}
}
As simple as that. It can now be extended to include custom event data and therefore a new class inheriting from EventArgs
and therefore ending up with a new delegate which can handle this new EventArgs
.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.