You see them everywhere, every control has them and they seem useful, but beginners don't always understand their nature.
article is a step-by-step guide to making and using events, the way the
visual studio makers implemented them in their controls, so if you too
want those shiny yellow lightning functions of your own keep reading .
Events aren't a must-have in any application, you can simply make a
function that does something and call it, events are just there, in my
opinion, to organize your code a bit (and to make you feel 1337 for
having events in your code :P).
So anyway, here's the way it's done.
Using the code
made a small project with everything you need to know and commented the
important stuff, you should be able to understand it all from there so
I'll explain it briefly here:
Step 1 - Create a delegate
those fancy delegates you always see and never understand what they're
for, well, in simple words: a delegate is a function that calls another
function, or if you'd prefer, here's a definition from akadia:
"A delegate in C# is similar to a function pointer in C or C++.
Using a delegate allows the programmer to encapsulate a reference to a method inside
a delegate object. The delegate object can then be passed to code which can call the
referenced method, without having to know at compile time which method will be
Anyway, here's an example of defining the delegate:
public delegate int GotSum(int a, int b);
Not complicated, this is going to take in two integers as parameters and return their sum.
Step 2 - Create an event
Here's the important bit - making the event itself. well, an event has a fixed structure:
[attributes] [modifiers] event [type] name
Attributes: public/private etc.
modifier can be one or a combination of the following keywords: public,
private, protected, internal, abstract, new, override, static, virtual,
event is the keyword you must have, and Type is the type of the event (in this case "GotSum", the delegate we defined earlier).
So now we define our event:
public event GotSum GetSum;
Step 3 - Initializing the event
an event to work, you need to...let's call it "bind" it to a function,
like you always see, you use += in order to do that (you can also use
the 'add' and 'remove' keywords for the event but I won't be covering
that in this article):
this.GetSum += new GotSum(Form1_GetSum);
Simple enough, I'm guessing you've done this many times before.
Step 4 - Making a function to call the event
this isn't required, you don't HAVE to do it, but that's just the way
the visual studio makers did it in their controls (for example you have
an OnClick function and a Click event), so here's how I did it:
public int OnGetSum(int a, int b)
return (this.GetSum(a, b));
Note that since the event itself returns an integer, this function returns an integer too (or else it'd be useless).
Now, by using
call the event with the two integers it requires as parameters, this
could be done directly from the function calling OnGetSum but I just
wanted to show you how it's done.
The function called when GetSum is fired is Form1_GetSum (the one we defined before), here it is:
int Form1_GetSum(int a, int b)
return (a + b);
simple, it just returns the some of the two integers (if you don't
understand this, you shouldn't be reading this article).
Step 5 - Calling the function
won't put the entire code here since it's irrelevant, you can see it
all in the project attached, but this is the code bit doing it all:
simple as well, it calls the OnGetSum function with two integers (if
you want to see what FirstNumber and SecondNumber are, just download
the example project).
Points of Interest
Well, nothing else to say, I hope this helped anyone who needed to
know it, if you have any questions/comments/suggestions just reply here.
Hope anyone who found the last title offensive is satisfied now.
11/06/2007 - Article re-posted.