tags:

views:

289

answers:

9

Using .NET how do you explain events to a beginner?

Most of the introduction books I've looked at talk about a WinForms app, double click the button in designer and viola you have an event.

I don't like it since it doesn't explain anything about what's happening behind the scenes or the more complicated things like chaining events.

Is there a better way to explain events and what should I be adding (for example chaining, delegates etc...)?

+7  A: 

There is a good article here. It starts with "An event is a mechanism via which a class can notify its clients when something happens. For example when you click a button, a button-click-event notification is sent to the window hosting the button." Which explains things quite well I think.

Simon P Stevens
A: 

An event is a signal that is sent from an object to inform the outside world about something (an event) that has happened or is about to happen. It provides a mechanism for the object to communicate this without needing to know who is listening (or if anyone is listening).

Fredrik Mörk
+2  A: 

Something like: An event is a message "to whom it may concern" that "something" happened (f.i. a click on a button or a timer elapsed). With a "handler" you can react to that event. You can subscribe to an event so your handler will get called when that event happens. Multiple classes can subscribe to a single event.

Hans Kesting
+5  A: 

If you're looking for real world examples, you could use twitter/RSS etc .

e.g. You post something on twitter, anyone that subscribes to your feed sees the post.

An object raises an event (=twitter post), any method that handles that event (=twitter subscribe) executes.

You can then talk about how you setup handlers (subscribe to accounts) why you should unsubscribe etc all with respect to twitter.

Binary Worrier
+1  A: 

An event is like a string pulling device. Anyone who is interrested can attach a string to the device. When something specific happens the device will pull the strings, so that anyone who is interrested gets a signal that it has happened.

:)

Guffa
A: 

An event is something the object raising it broadcasts:

Analogy: Think of an alarm light on a well house for high water.

C#: This is the System.Windows.Forms.Button.Click event

Delegates are attached to this event, they care about when this event happens.

Analogy: They are like the maintenance persons, but perhaps not those from the next town over.

C#: This is the method in your code which "does something when Click happens". You first "care about the event" with myButton.Click += new EventHandler( myButton_Click ). And the worker is the method myButton_Click( Object sender, EventArgs e ) itself. Multiple worker methods may "care about" the same event.

maxwellb
A: 

Why the technical jargon?

There's an idiom everyone knows by now:

http://www.glenbrook.k12.il.us/GBSSCI/PHYS/CLASS/newtlaws/u2l4a.html

TFM
A: 

In Delphi - I have added/defined and implemented additional event handlers and because I grew up in the 60's I called these "Happenings". I think the word describes it better than "event". So the analogy runs that "happenings" were mostly NOT PLANNED they just happened - you do not normally code them in as part of a pre-defined program execution path - you have to wait for them to occur.

To be involved or invited you had to have the right contacts. And Adding yourself to the Happening notification list (via your objects' event methods) is a way of making sure you get to be part of the happening ... Or "there is a Happening going on at this object" - Do you want to be involved or not. If you want to be involved notify your contacts then you get to go to the party :))

Just my light hearted two penneth :))

Despatcher
A: 

Trivial usages of events, such as those in most examples, could be replaced with an implementation that doesn't use events and/or involve interacting with some kind of black box interface (System.Web.Page, etc).

If you really want to teach someone events, find a scenario where they are both the producer and the consumer of the event, and that the events provide substantial benefits over function/method calls. If you want to understand events, you not only have to grasp what they do but understand where they can be used to advantage.

quillbreaker
I think I agree with this. Do you have any examples?
maxwellb
Isn't there a lightweight version of System.Web.UI.Page used for AJAX? You could have a propsective student reimplement some of Page's functionality.
quillbreaker