The event model means you have a class that has events ( for example a button that can be clicked. The Click is an event ). Now you can register so-called Event-Handlers with these events ( in this example a function that gets called when the button gets clicked ). A single event my have more than one event handler attached. Each attached event handler will be called. However, there is no fixed sequence, the order of the event handlers can be random ( example: if you attach two functions to the click of the button, you can be sure both will be called when the button is clicked. However, you can never be sure which one will be called first ).
Code:
public delegate void pqr(int p);
This is a declaration of a delegate. In C++ this would be a function pointer. It's a type which can encapsulate functions so that these functions can later be called.
some class to have a main method:
Code:
class z
{
static void Main()
{
Create a new instance of class x, as described later
This class has an event c. To attach an event handler to this event, we create a new delegate ( see type above ) with the instance and function to call.
Code:
a.c += new pqr(a.pqr1);
Now there is one event handler attached to the event. We call a method on the class that will trigger the event. The following line will result in a.pqr1 getting called because this was attached as an eventhandler for the event that will occur
We will now add a second event handler. As I said earlier, BOTH will get called, but not neccessarily in any specific order
Code:
a.c += new pqr(a.pqr2);
We raise the event again, the result should be that BOTH, a.pqr1 AND a.pqr2 are called, maybe 1 first, maybe the other first, thats not defined.
This class contains the event
Code:
class x
{
public void pqr1(int j)
{ System.Console.WriteLine("pqr1 " + j); }
public void pqr2(int j)
{ System.Console.WriteLine("pqr2 " + j); }
The latter where just two normal functions printing the int that was given as a parameter. The following is the declaration of an event. It's type is declared above. This means: This class has an event, which can be handled by functions that match the type of pqr ( earlier defined as delegate to functions that return void and take an int ).
Code:
public event pqr c;
The following function will raise the event with the number 10, which will result in all attached event handler functions getting called with 10 as parameter.
Code:
public void abc()
{
c(10);
}
}
I have some personal comments for this file: If you did it yourself for testing, it's ok, you're still learning and I don't mean to criticise you. If this was a tutorial or help of some kind, it's badly written. For one, Eventhandlers always have two parameters, and object named sender and an EventArgs parameter named e ( Microsoft Class library design guidelines ). However, this is just a definition and not everyone might want to take that serious. But the event is called without any checks by the abc function. While in this example it works, it will crash if you don't have any event handlers attached. As the nature of events is that the sender will not know if or how they will be handled, checking this is a basic neccessity.
replace c(10); by if( c != null ) c(10);
This sample is a bit too complex. You only need one class to demonstrate event handlers and having the event and the handler in the same class is perfectly valid, but maybe a bit confusing as it's against the event/handler concept.
If this was a tutorial, get a way better one. If it was you testing stuff out, just disregard my ramblings