473,386 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 2022
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Steven Brown | last post by:
I'm trying to figure out how to safely use .NET events/delegates in a thread-safe class. There are a couple problems. One is that the standard "if(EventName != null) EventName(...);" call can...
1
by: Jack Addington | last post by:
I have a 3rd party object that fires an itemchanged event when someone edits some data on a form. This event has a custom eventArgs that has a field called ActionCode. In the code of the event,...
4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
4
by: DKode | last post by:
Hello, I have a question about delegates and events I have a basic understanding of each, but in the apps I'm building I'm never needing to use these tools. I'm wondering when and where you...
3
by: Chris | last post by:
Hi, what is the difference between using events and delegates (apart from the syntax) ? have a look at following (working) programs please (you can just copy/paste and build it) : First...
15
by: Bryce K. Nielsen | last post by:
I have an object that starts a thread to do a "process". One of the steps inside this thread launches 12 other threads via a Delegate.BeginInvoke to process. After these 12 threads are launched,...
2
by: kristian.freed | last post by:
Hi, I currently work in a project written fully in C# where we make extensive use of delegates and events. We have a model where a "state", an object holding data but not much code but which...
6
by: Smithers | last post by:
Just looking to compile a list of "all the ways to implement events". I'm NOT looking to get into the merits or mechanics of each in this thread... just want to identify them all - good, bad, and...
7
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.