« ASP.NET AJAX Version 1.0 Released | Main | Video: Viewing Albums with WPF/E and ASP.NET AJAX »

Video: Creating Custom Events and Delegates with C#

It's no secret that events and delegates play a crucial role in the .NET framework. Without them it would be hard to handle user input or notify other objects when an action occurs. I get a lot of questions about events and delegates in classes I teach so I decided to put together a video that outlines the fundamentals of creating a custom class that exposes an event and a delegate. The video also demonstrates how to create a custom EventArgs class and how events can be consumed using C#. The code shown in the video can be downloaded here.

 

Posted on Thursday, January 18, 2007 at 12:51AM by Registered CommenterDan Wahlin in , | Comments15 Comments

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments (15)

Nice tutorial, though the standart way is to define protected virtual method OnEventName, so as you said we can override it.
January 18, 2007 | Unregistered CommenterEugene
Hi, is there any way I can download the video? My internet here is slow for viewing online. Thanks :) Nice stuff.
January 19, 2007 | Unregistered CommenterChua Wen Ching
Chua,

Right-click on the link [Click here to view in Windows Media Player] and choose "Save Target As..." from the context menu.

This lets you save the .WMV file to your computer.
January 19, 2007 | Unregistered Commentera e shaw
I see. Thanks :)
January 19, 2007 | Unregistered CommenterChua Wen Ching
I want to thank you for creating this video, it really makes understand the subject much easier when it is demonstrated and you do a good job of explaining it.
January 20, 2007 | Unregistered CommenterAbyss
Hi Dan,

Nice example. You could even leave out the line where you define the StageCompletedHandler, by using generic EventHandles (.NET 2.0).

Then instead of using:
public event StageCompletedHandler StageCompleted;

you can do this:
public event EventHandler<StageCompletedEventArgs> StageCompleted;
January 26, 2007 | Unregistered Commenterdion
I considered using the new generic EventHandler but figured it would be good to show the "standard" way since some may be using .NET 1.1 at this point. Thanks for commenting on it since now people can see both ways. For those doing .NET 2.0 apps I'd definitely use what Dion lists especially since it'll save you some code.

As far as the comment about protected with OnEventName, you're quite right. I caught that after the video was made but didn't want to go back and re-do it again to be honest since it takes some time to get right. :-)
February 5, 2007 | Registered CommenterDan Wahlin
Hi Dan,

I had the last step everytime wrong. But now I know, how is running. Thanks so lot.

Horanyi
February 13, 2007 | Unregistered CommenterJanos HORANYI
No problem...glad you enjoyed the video. :-)
February 16, 2007 | Unregistered CommenterDan Wahlin
Nice, vid. Helped ALOT.
May 13, 2007 | Unregistered CommenterMilo
Hi Dan Wahlin

At last time I couldn't do that event handale as customizly.but after seen this vid I can do that.So it is very usefull for me.so thanks a lot

Anushka K Rajasingha
May 23, 2007 | Unregistered CommenterAnushka K Rajasingha
Great tutorial, even for starters like me

thanks
June 4, 2007 | Unregistered CommenterME
Thanks for the video.


Read through several books to understand what delegates are. All were confusing.

This video clarified everything.


Thanks a lot.
June 28, 2007 | Unregistered CommenterShane
Fantastic job with this video! I have spent days trying to figure delegates and events, and this clarified what I needed.

One thought - I needed to implement this for a web user control I had built, and as referenced above the event came along AFTER the page had finished rendering. I sorted that by resetting the event handlers in the Page_Load event, which is called before the event is serviced in the control. I'm sort-of nervous about this, however it appears to work just fine and is a heck of a lot simpler than using async methods.

Good job - well done! Andrew
August 4, 2007 | Unregistered CommenterAndrew
Thank you so much for the video. I have spend so much time off and on trying to get this right. ...then when I think I have it right something just doesn't seem exactly correct.

Everything now seems to be working normally (i.e. like eveything else does in C#).

Well done!.

-W

August 6, 2007 | Unregistered CommenterDizzy
Editor Permission Required
You must have editing permission for this entry in order to post comments.