471,334 Members | 2,463 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Don't understand delegates

Tem
I've read every example i could find on the subject and still couldn't
figure out its proper usage.

What's the point of delegates, why can't I just invoke the method
directly???

Can someone please help?

Tem

Jun 27 '08 #1
6 1460
Delegates are pointers to methods. They are a way of having a method as a
variable which can be passed around. They are handy for dependency injection
and things. For example. I have some classes which initialise my business
layer at the start of my application. They take a Delegate to a method like
void ReportStatus(string)
The function call this delegate passing a string which states what they are
currently doing for initialisation. This allows be to change how they report
their information by passing in different methods.
The windows forms app passes in a reference to a method which updates a
label on the splash screen. The command line version passes in a method with
prints to the command line. and potentially a server version could pass one
in which logs to a file/database etc and if i dont want to log at all i can
pass a null delegate
The good thing about this is that the logic for logging is seperated
entirely from the logic for initialisation so they are changeable and
testable independantly.

Does this help you see the usefullness of them?
--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"Tem" wrote:
I've read every example i could find on the subject and still couldn't
figure out its proper usage.

What's the point of delegates, why can't I just invoke the method
directly???

Can someone please help?

Tem

Jun 27 '08 #2
This is effectively how events work. In a normal situation like the below,
you could use an event which is effectively a list of delegates to call when
the event is raised. There is a reason in the below example why it isnt an
event but its not important for this discussion.
--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"Ciaran O''Donnell" wrote:
Delegates are pointers to methods. They are a way of having a method as a
variable which can be passed around. They are handy for dependency injection
and things. For example. I have some classes which initialise my business
layer at the start of my application. They take a Delegate to a method like
void ReportStatus(string)
The function call this delegate passing a string which states what they are
currently doing for initialisation. This allows be to change how they report
their information by passing in different methods.
The windows forms app passes in a reference to a method which updates a
label on the splash screen. The command line version passes in a method with
prints to the command line. and potentially a server version could pass one
in which logs to a file/database etc and if i dont want to log at all i can
pass a null delegate
The good thing about this is that the logic for logging is seperated
entirely from the logic for initialisation so they are changeable and
testable independantly.

Does this help you see the usefullness of them?
--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"Tem" wrote:
I've read every example i could find on the subject and still couldn't
figure out its proper usage.

What's the point of delegates, why can't I just invoke the method
directly???

Can someone please help?

Tem
Jun 27 '08 #3
On Apr 25, 2:48*am, "Tem" <tem1...@yahoo.comwrote:
I've read every example i could find on the subject and still couldn't
figure out its proper usage.

What's the point of delegates, why can't I just invoke the method
directly???
Because maybe you do not know which exact method to call. Or maybe the
exact method to call change dynamically.

One way of think of it as if your class is incomplete and is the code
using it who is going to complete it.
Jun 27 '08 #4
Ignacio points out the primary reason: You are not sure what you are calling
until runtime.

Delegates are aslo useful for event handlers, when you only want a specific
bit of code called in a certain instance.

Another reason to delegate is callbacks, which cannot be coded directly as
method calls. I actually restated reason #2, only in different language, and
reason #1, at least in some instances. :-)

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

*************************************************
| Think outside the box!
|
*************************************************
"Tem" <te*****@yahoo.comwrote in message
news:eR**************@TK2MSFTNGP02.phx.gbl...
I've read every example i could find on the subject and still couldn't
figure out its proper usage.

What's the point of delegates, why can't I just invoke the method
directly???

Can someone please help?

Tem

Jun 27 '08 #5
On Fri, 25 Apr 2008 08:09:18 -0700, Cowboy (Gregory A. Beamer)
<No************@comcast.netNoSpamMwrote:
Ignacio points out the primary reason: You are not sure what you are
calling
until runtime.
Careful with the "until runtime". _Some_ code usually knows what's going
to be called at compile time. It's just that the _calling_ code doesn't
know until runtime.

With that in mind, I'd say that technically speaking, all uses of
delegates fall into this category. For example...
Delegates are aslo useful for event handlers, when you only want a
specific
bit of code called in a certain instance.
But the reason they are used for event handlers is that at compile time,
the implementer of the event doesn't know what code will be called. The
..NET Forms classes are a classic example of this. Microsoft can't
possibly know what your own code would be when they provide an event. So
using a delegate allows other code to provide the reference to the method
to be called, well after the point at which Microsoft's code was compiled.
Another reason to delegate is callbacks, which cannot be coded directly
as
method calls.
That's not strictly speaking correct, depending on what you mean by "as
method calls". Java doesn't have any idea like delegates, and yet it can
implement the same sort of behavior. It uses interfaces instead. And
those are coded directly as method calls (i.e. a method defined in an
interface implemented by whichever class wants to provide the callback).
This could be done in .NET, and in fact for more elaborate APIs is in fact
used quite a lot.

But regardless, that's still a subset of the general "you don't know at
compile time the exact method that will be called".

I prefer delegates, and I feel that for single-method situations they are
way more convenient than having to create a whole interface and then
implement it in each class that wants to provide a method to call at
specific times (whether to support events, i/o callbacks, whatever). But
you don't _have_ to have delegates to allow for callbacks that aren't
known at compile time.

Pete
Jun 27 '08 #6
Tem... You can use delegates when you need "runtime polymorphic"
behavior.

Runtime polymorphism involves programming to a type, such that the
implementation details can be different and are discoverable at runtime.
When
programming to a type, the caller does not know the class of the object,
only
that the object implements the type of interest.

http://www.geocities.com/jeff_louie/OOP/oop37.htm
http://www.geocities.com/jeff_louie/OOP/oop31.htm

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Jeffrey T. Smith | last post: by
4 posts views Thread by Tim | last post: by
2 posts views Thread by hangaround | last post: by
9 posts views Thread by raylopez99 | last post: by
reply views Thread by rosydwin | last post: by

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.