Hi
Why do most .NET Framework events take the following form:-
Page_Load(Object sender, EventArgs e)
Looking at the Event example in the MSDN documentation it is as follows:-
// Declare the Delegate
public delegate void SampleEventHandler(Object sender, EventArgs e)
// Declare the Event
// public event SampleEventHandler SampleEvent;
//Raise the Event
SampleEvent(this, e);
So given Page_Load is an Event it doesnt fit any of the above syntax. The only one that it is similar to is the delegate but of course this is a Delegate.
I just need a bit of clarification please. I know the Page_Load is the Event Handler Method so is this used differently to the above as the Method has its own Object sender, EventArgs e parameters?
Regards
Steven
Loading
VulpesPosted Jun 21, 2012, 1:52 PM
using System;
class Test
{
static void Main()
{
Test t = new Test();
Page p = new Page();
p.Load += new EventHandler(t.Page_Load);
p.OnLoad(t, EventArgs.Empty);
Console.ReadKey();
}
public void Page_Load(Object sender, EventArgs e)
{
Console.WriteLine("Hello from Page_Load");
}
}
class Page
{
public event EventHandler Load;
public void OnLoad(Object sender, EventArgs e)
{
if(Load != null)
{
Load(sender, e);
}
}
}
If you change the highlighted line to:
Load.Invoke();
then you'll find that the program won't compile. However, it will compile and run fine if you use this:
Load.Invoke(sender, e);
which is what the compiler (in effect) translates C#'s special syntax: Load(sender, e) into.
VulpesPosted Jun 21, 2012, 5:01 PM
The Page_Load eventhandler must be compatible with the System.EventHandler delegate. So it must take two parameters of types Object and EventArgs and have a return type of void.
Guest UserPosted Jun 21, 2012, 4:03 PM
The signature of the event-handling method is identical to the signature of the event-handler delegate.
The event-handler signature observes the following conventions:
The return type is Void.
The first parameter is named sender and is of type Object. This is the object that raised the event.
The second parameter is named e and is of type EventArgs or a derived class of EventArgs. This is the event-specific data.
The method takes exactly two parameters.
So from this it implies that the EventHander delegate has the two parameters and not the EventHandling method, however Page_Load still does?
Is this because the Page_Load Method has to match the Delegate signature ie Return Type and Parameter Types?
Guest UserPosted Jun 21, 2012, 3:27 PM
So can the 2 parameters (Object sender, EventArgs e) which are commonly seen be either declared on the Delegate or the Method being invoked by the Delegate ie:-
// Delegate declaration
delegate void EventHandler(Object sender, EventArgs e)
or
//Method declaration
protected void Page_Load(Object sender, EventArgs e)
or
Both?
Regards
Steven
VulpesPosted Jun 21, 2012, 2:56 PM
http://msdn.microsoft.com/en-us/library/system.eventhandler.aspx
Guest UserPosted Jun 21, 2012, 2:35 PM
For example
public delegate void EventHandler(Object sender, EventArgs e);
public event EventHandler NoDataEventHandler;
I assume that either a delegate or method can be used as the "EventHander" ie your example or this one?
Thanks
Guest UserPosted Jun 21, 2012, 10:34 AM
Am I right in saying that because the Page_Load Event Handler Method already has the 2 parameters Object sender and EventArgs e is it satisfactory to do this:-
// Declare the Delegate
public delegate void EventHandler();
// Declare the Event
public event EventHandler Load
// Add the Page_Load Method to the Invocation list
Load += new EventHandler(Page_Load);
// Invoke
Load.Invoke();
This being different from the other way of specifying a Delegate which has the Object sender, EventArgs e as the 2 parameters like so...
// Declare the Delegate
public delegate void EventHandler(Object sender, EventArgs e);
// Declare the Event
public event EventHandler Load
// Invoke
Load(this, e);