Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 3rd, 2005, 6:23 PM   #1
melbolt
Hobbyist Programmer
 
melbolt's Avatar
 
Join Date: Feb 2005
Location: PA, USA
Posts: 237
Rep Power: 4 melbolt is on a distinguished road
Send a message via AIM to melbolt Send a message via Yahoo to melbolt
question: usage of delagates and custom events

hi there,

I feel like i've been making some good progress with my learning in .NET programming, however i'm kind of stumped on when and how to use delegates and custom events in my programming. I've read several tutorials but it's still fuzzy to me, i was just wondering if anyone can give me a basic overview of when these might be used, what exactly they are used for, and what the differences between the two are.

any feedback is greatly appreciated.

this has been a brick wall in my understanding of programming concepts,

thanks
__________________
I have never let my schooling interfere with my education. -Mark Twain-

Xbox live gamertag: melbolt
melbolt is offline   Reply With Quote
Old Oct 3rd, 2005, 7:17 PM   #2
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
I'll try to break it down.

1. An event is more or less a list of delegates. When you raise an event, all the event handlers (which are delegates) are invoked.

2. A delegate is like a function prototype. In the case of events, it specifies what parameters the event handlers take.

An example is if you had an AlarmClock class, which happens to be an example in the MSDN.

3. Your alarm clock would have an Alarm event, an AlarmEventHandler delegate, and an AlarmEventArgs class, following the typical .Net pattern (though you don't have to).

4. The Alarm class has an OnAlarm method that allows subclasses to both raise (subclasses can't call the event directly) and handle (overriding is cleaner than an event) the Alarm event. Note that AlarmEventHandler says that any handlers have to have a sender parameter (a reference to who raised the event, since a single handler often serves many objects) and a paramter of type AlarmEventArgs (derived from EventArgs and containing data specific to an Alarm event).

When you want to add a handler on the alarm:
Alarm anAlarm = new Alarm();
anAlarm.Alarm += new AlarmEventHandler(myHandler); //Adds a delegate pointing to myHandler, which matches the prototype for AlarmEventHandler

When the Alarm needs to go off (called from inside Alarm)
OnAlarm(new AlarmEventArgs(<insert stuff relevant data>);

Inside OnAlarm:
if (Alarm != null)  //An event can be null. We don't want an exception.
   Alarm(this, theEventArgsParameter); //Passes the sender and event args.

Your Handler called myHandler (Which matches the AlarmEventHandler delegate):
MessageBox.Show("Alarm has gone off!");

Now, take a look at the examples for events provided by MSDN and see if you can understand what's going on. Then, create a project with a form (A Windows Application in Visual Studio), and see what the form designer adds when you tell it to add an event handler.

If you have specific questions, seeing how broad Events are, please elaborate.

As for when to use them: Whenever an event occurs (surprised?) that one or more parts of your code needs to be made aware of, but the originator of the event doesn't know who wants to know about it. Look at a Button control. Does the button know what parts of your code to call when it is clicked? No. A perfect canidate for an event, which is why there is a Click event on a Button.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:48 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC