?
Solved

NEWBIE: Do events and delegates inform, or do?

Posted on 2006-05-05
7
Medium Priority
?
346 Views
Last Modified: 2010-04-16
Dear Experts,

I'm trying to understand the basics of delegates and events in C#.

Assume I have two classes which interact with a delegate/event model.

ButtonClass:  Encapsulates a button, can be clicked.

CaresAboutClicksClass:  Does something when the button is clicked.

I had (vaguely) assumed that when ButtonClass registers a click, it somehow just broadcasts this information, and CaresAboutClicksClass somehow hears the broadcast--perhaps with an assist from the .NET runtime.

What I'm reading seems to say, though, that  CaresAboutClicksClass actually passes a METHOD--which is now a full-fledged object--to ButtonClass, initially.  Then, when ButtonClass registers a click, it doesn't somehow just TELL CaresAboutClicksClass that a click happened.  ButtonClass actually INVOKES the METHOD DIRECTLY.  CaresAboutClicksClass doesn't actually DO anything at that point.

Is that right?

If it is right, does it make any sense to ask "where is this method being called FROM?".

Thanks!
BrianMc1958
0
Comment
Question by:BrianMc1958
7 Comments
 

Author Comment

by:BrianMc1958
ID: 16618165
A shorter version of the same question:

The subscriber isn't really saying, "When this happens, TELL me".  The subscriber is saying, "When this happens, DO this".

Is that right?
0
 
LVL 9

Accepted Solution

by:
pallosp earned 200 total points
ID: 16618324
Multiple methods can be subscribed to an event. When the event happens, the CLR calls every method in undefined order.
The important thing is that the event handling process is done by the intermediation of the CLR, not by direct callback.
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 200 total points
ID: 16618338
Basically the ButtonClass would expose an event. CaresAboutClicksClass registers to be notified about the event by passing a method that matches the signature of the delegate associated with the event. When ButtonClass raises the event it calls the correct method of any object that has subscribed to the event.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Assisted Solution

by:krupini
krupini earned 200 total points
ID: 16618395
That is right. ButtonClass has a "Click" event, which is a function pointer to keep things simple. So when the button is clicked, whatever function the "Click" function pointer is pointing to will get executed.

Consider this pseudo-code:

Button.Click = someOtherObject.TakeCareDaClicks();

So when the button is clicked, someOtherObject.TakeCareDaClicks() is executed because the Click function pointer is pointing to it.
0
 
LVL 7

Assisted Solution

by:CraigHarris
CraigHarris earned 200 total points
ID: 16618901
A delegate is a typesafe method pointer.

The event handling code in CLR will  call the methods, as indicated by delegates, when required.

So neither of the classes originally mentioned above does the actual calling -- even the Click event of the button is handled in the same way (Delegates can be multicast and result in multiple method called upon invokation.)
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 200 total points
ID: 16618917
0
 

Author Comment

by:BrianMc1958
ID: 16630433
Thank you, everybody.  That helps.  And I'll post a follow-up...

--BrianMc1958
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question