Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to use delegates??

0 views
Skip to first unread message

KA Kueh

unread,
Feb 9, 2001, 9:51:33 PM2/9/01
to
Dear all,

I have read through the documentation and sample codes of C# but still do
not understand the use of delegates. Can someone provide me with a simpler
explaination as to the use and why/how is should be placed in the coding
scheme of C# ?. BTW, I do have understanding for most high level languages
like VB, VFP, but not proficient in C. Thanks.

Regards,
Kueh.


Courageous

unread,
Feb 9, 2001, 10:14:57 PM2/9/01
to

Hrm. A "delegate" is a sort of surrogate object which allows you
to invoke it like a function, but keeps implicit track of certain context
information associated with itself. So, supposing you had an instance
of a car with a start() method. If, for some reason, some other section
of the program needed to be able to start your car, when it tried to do
so, the various attributes of the car would be associated with the
execution of the start() method.

C//

Jesse Liberty

unread,
Feb 10, 2001, 1:23:51 PM2/10/01
to
Here's an analogy I use in my (forthcoming) book Programming C#:
When a head of state dies, the President of the United States typically does
not have time to go to the funeral personally. Instead, he dispatches a
delegate. Often this delegate is the Vice President, but sometimes the VP is
unavailable and the President must send someone else, such as the Secretary
of State or even the First Lady. He does not want to "hard wire" his
delegated authority to a single person; he may delegate this responsibility
to anyone who is able to execute the correct international protocol.

The president defines in advance what authority will be delegated (attend
funeral) what parameters will be passed (condolences, kind words) and what
value he hopes to get back (good will). He then assigns a particular person
to that delegated responsibility at "run time" as the course of his
presidency progresses.

In programming, we are often faced with situations where we need to execute
a particular action but we don't know in advance which method, or even which
object we'll want to call upon to execute that action. For example, a button
might know that it must notify some object when it is pushed, but it may not
know even until run time which object (or objects) wish to be notified.
Rather than wiring the button up to a particular object at compile time, we'
d like to connect the button to a delegate and then resolve that delegate to
a particular method when the program executes.

Delegates are typically used to handle events and you can use delegates as
call-backs; so that one class may say to another "do this work and when you'
re done, let me know." For example, you can pass a delegate to the
asynchronous I/O methods and they will invoke the delegated method on
completing the read/write.

Hope that helps.

-j


--
Jesse Liberty, President
Liberty Associates, Inc.
http://www.LibertyAssociates.com

"KA Kueh" <k...@ksm.com.my> wrote in message
news:uauQ#rwkAHA.1932@tkmsftngp05...

Joseph Albahari

unread,
Feb 10, 2001, 2:19:07 PM2/10/01
to
A delegate type is a definition of a method signature. It defines the
parameters a method must accept, and the type, if any, it must return.

A delegate object (created from a delegate type) is a pointer to a method
conforming to the delegate signature.

Imagine we have a collection of methods that encode a character into an
integer. Here's a delegate to define the common signature:

delegate int Encoder (char c); // a delegate declaration

This says "all delegate objects of type Encoder must point to a method that
accepts a char parameter, and return an int".

Here are a couple of methods which conform to this signature:

int SimpleEncoder (char c)
{ return c.ToInt32(); }

int OffsetEncoder (char c)
{ return c.ToInt32() + 100; }

and here's how we create a delegate object, myEncoder, pointing to
SimpleEncoder:

Encoder myEncoder = new Encoder (SimpleEncoder);

and this is how to calll the delgate:

int myCode = myEncoder ('A'); // returns 65

Of course, the above two lines would compile just as well if myEncoder
pointed to OffsetEncoder.

*** Note that a delegate has no code!!! ***

Delegates are used primarily for event handling. Let's say you have a
button on a form and you want to attach to its Click event a method you've
written called called button1_Click. If C# didn't have delegates, this is
probably how they'd make you do it:

button1.Click += AddressOf (this.button1_click);

In reality, button1_click is going to expect parameters. It'll need to know
the source of the event (because we might want to have more than one button
firing the same method), and possibly other information such as the mouse
position. Imagine the code inside the Button's .NET class definition, at
the point where it decides that it's been clicked, and wants to fire its
Click event, passing this information:

Click (this, myEventArgs);

where "this" is the source of the event, and myEventArgs is an object
containing information such as the mouse position. We're firing the event
with two parameters - one of type Button and one of type EventArgs. The C#
compiler would just have to *trust* that when the developers hooks up
button1_click it also accepts two parameters of matching types (ie has the
same signature). It would have no way to enforce this at compile-time, and
this would run against the whole strict type system of C# and .NET.

Enter delegates and formal method signature declarations. We define a
delegate called EventHandler as follows:

public delegate void EventHandler (object sender, EventArgs e);

and in the definition for the Button's Click event in .NET, we tell the
compiler only to allow it to hook up to methods conforming to the
EventHandler delegate:

public event EventHandler Click;

We attach our button1_Click handler as follows:

button1.Click += new EventHandler (this.button1_Click);

I agree that the use of delegates is one the hardest topics in C# to
understand. You won't have come across it in VB and VFP. VFP is much
simpler in that, in a sense you implicitly subclass whenever you put a
control on a form. Hence if you put a CommandButton on a form, all you need
to do is override the subclassed control's Click() method. And then the
call to the parent code is implicit (you actually have to use the NODEFAULT
keyword to stop it).

Regards,

Joseph Albahari


"KA Kueh" <k...@ksm.com.my> wrote in message
news:uauQ#rwkAHA.1932@tkmsftngp05...

0 new messages