470,615 Members | 2,209 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,615 developers. It's quick & easy.

Events, delegates, callbacks and methods

I'm confused:

"why do we need to assign a method to a delegate then assign the delegate to
an event why not just assign the method to the events event handler"

and

"how does making a callback to some method differ from calling the method as
per normal"?

If anyone's got a minute or two to explain this stuff to me I'd be really
appreciative (even a pointer to an online paper would do).

Confused,
Michael

--

This email has been scanned with Norton AntiVirus 2003.
Nov 15 '05 #1
8 1915
Hi,

you do not have to assign a delegate an event. You create events of some
delegate type.

You declare delegates (just a special class implicitly inherited from
Delegate, or MulticastDelegate) to specify how the methods assigned to this
delegate should look like (also called a signature). They also contain a
list and some other stuff to hold the references to the methods assigned to
them, so they can fire them at some later point in time.
So you are actually subclassing Delegate or MulticastDelegate when you are
declaring a delegate (a little bit compiler magic here).
You can instantiate delegates like any other type, assign your methods to
it, fire them and so on.

Now, when you declare an event (of some delegate type), you are just
creating a delegate of that type, so it can hold references to methods (with
the same signature). It only looks different. The reason why you need to
add the 'event' keyword is to let the compiler know it is an event, so it
shows up in the property inspector (in case of a component). The compiler
also generates two hidden methods to add and remove methods to the event (or
delegate if you like). That's why you can use the += and -= syntax.
The compiler will also add a hidden delegate instance which will actually
hold the references assigned to the event. Again compiler magic.

So, an event is more or less the same as a delegate instance, with some
hidden extras to make it visible to the designer and to make it easier to
use (+= and -=).

Hope this helped a bit,

Bram.


"Michael McDowell" <mi*****@triggerwave.co.uk> wrote in message
news:Oe**************@TK2MSFTNGP09.phx.gbl...
I'm confused:

"why do we need to assign a method to a delegate then assign the delegate to an event why not just assign the method to the events event handler"

and

"how does making a callback to some method differ from calling the method as per normal"?

If anyone's got a minute or two to explain this stuff to me I'd be really
appreciative (even a pointer to an online paper would do).

Confused,
Michael

--

This email has been scanned with Norton AntiVirus 2003.

Nov 15 '05 #2
"Michael McDowell" <mi*****@triggerwave.co.uk> wrote:
"how does making a callback to some method differ from calling the method as
per normal"?


At what level?
Nov 15 '05 #3
Hi,

forgot about the callbacks,

Callbacks are used when you need someone else to call a method (the callback
function or method) for you.
A good example is the Thread class. A thread needs a method to execute. It
has to do something, right ?
This method is passed to the Thread constructor by means of a delegate,
pointing to the method to run.
We usually don't call it a callback, it's more a thread function or thread
method, but you could look at it as a callback function.

Another example is the IComparer interface. When you sort an array
(Array.Sort), you can specify an interface to be used to compare the array
elements while sorting. So Array.Sort calls the Compare method of the
IComparer interface. Also some kind of a callback situation; it get backs
to you to do the actual comparison.

That's how the name 'callback' was *invented*. You just give some other
piece of code a method to get back to you while it's doing its thing, to get
some information from you, or to have you do something yourself, or
whatever...

hope you're a little bit less confused now,

Bram.


"Michael McDowell" <mi*****@triggerwave.co.uk> wrote in message
news:Oe**************@TK2MSFTNGP09.phx.gbl...
I'm confused:

"why do we need to assign a method to a delegate then assign the delegate to an event why not just assign the method to the events event handler"

and

"how does making a callback to some method differ from calling the method as per normal"?

If anyone's got a minute or two to explain this stuff to me I'd be really
appreciative (even a pointer to an online paper would do).

Confused,
Michael

--

This email has been scanned with Norton AntiVirus 2003.

Nov 15 '05 #4
Michael McDowell wrote:
"why do we need to assign a method to a delegate then assign the
delegate to an event why not just assign the method to the events
event handler"


The method called by the delegate is usually protected from outside sources.
Delegates solve the accessibility problem quite nicely.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #5
Michael,
"why do we need to assign a method to a delegate then assign the delegate to
an event why not just assign the method to the events event handler"


Do you mean why we have to type

obj.SomeEvent += new DelegateType(Handler);

rathern than simply

obj.SomeEvent += Handler;

? If so, I believe you'll be able to use the latter syntax in the
Whidbey release of C#.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #6
Mattias is correct.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:Ou**************@TK2MSFTNGP10.phx.gbl...
Michael,
"why do we need to assign a method to a delegate then assign the delegate toan event why not just assign the method to the events event handler"


Do you mean why we have to type

obj.SomeEvent += new DelegateType(Handler);

rathern than simply

obj.SomeEvent += Handler;

? If so, I believe you'll be able to use the latter syntax in the
Whidbey release of C#.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 15 '05 #7
Mattias Sjögren <ma********************@mvps.org> wrote:

<snip>
? If so, I believe you'll be able to use the latter syntax in the
Whidbey release of C#.


When's Whidbey out*, anyway? And is Whidbey the code-name for 2.0?

* i.e.: available for use in an IDE
Nov 15 '05 #8
When's Whidbey out*, anyway?
Don't know, but if marketing decides, probably six months before it's
actually ready. :-)

And is Whidbey the code-name for 2.0?


Yes, v2.0 of the framework and v8.0 (or 200?) of the VS IDE.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Steven Brown | last post: by
1 post views Thread by Jack Addington | last post: by
4 posts views Thread by DKode | last post: by
3 posts views Thread by Chris | last post: by
15 posts views Thread by Bryce K. Nielsen | last post: by
6 posts views Thread by Smithers | last post: by
7 posts views Thread by Siegfried Heintze | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.