Click here to Skip to main content
15,886,584 members
Articles / Programming Languages / C#
Article

A Beginner's Guide To Events

Rate me:
Please Sign up or sign in to vote.
2.10/5 (10 votes)
11 Jun 20073 min read 15K   125   8   4
An article for beginners explaining how to make those fancy events you always see on visual studio controls in 5 simple steps.

Introduction

You see them everywhere, every control has them and they seem useful, but beginners don't always understand their nature.

This 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 :).

Background

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

I 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

Yup, 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 invoked."

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.

The modifier can be one or a combination of the following keywords: public, private, protected, internal, abstract, new, override, static, virtual, or extern.

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

For 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

Note: 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)
{
    // Calls the event and returns what's returned from it.
    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 this.GetSum(a,b); you 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)
{
    // Returns the sum of the two integers.
    return (a + b);
}

Really 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

I 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:

this.OnGetSum(FirstNumber, SecondNumber);

Pretty 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.

History

11/06/2007 - Article re-posted.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Israel Israel
I'm a 16 year old guy, studying at some high-school in Israel.

Comments and Discussions

 
GeneralNot a good introduction Pin
User 20688511-Jun-07 7:44
User 20688511-Jun-07 7:44 
GeneralRe: Not a good introduction Pin
sharpiesharpie11-Jun-07 11:05
sharpiesharpie11-Jun-07 11:05 
GeneralRe: Not a good introduction Pin
User 20688511-Jun-07 11:11
User 20688511-Jun-07 11:11 
Well this sentence suggests otherwise :
"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)."

Anyway, I think any article, regardless of target audience, should use real situations as examples. This way, beginners can better understand the practical uses behind the theories they are learning.

modified 27-Feb-21 21:01pm.

GeneralRe: Not a good introduction Pin
sharpiesharpie11-Jun-07 12:05
sharpiesharpie11-Jun-07 12:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.