Help with delegates

  • Thread starter Thread starter OpticTygre
  • Start date Start date
O

OpticTygre

Ok, so I've been trying and trying to learn about delegates, but I still
can't grasp the concept of the advantages. Why use delegate functions
instead of calling the function directly?

In other words, if I had something like:

---------------------------------------------
Public Class ProductList

Public Function GetTotal() as Decimal
'Code omitted
End Function

End Class
---------------------------------------------

And then something like:

---------------------------------------------
Public Delegate Function GetTotalDelegate() as Decimal

Dim List As New ProductList()

Dim GetTotalPointer As New GetTotalDelegate(AddressOf List.GetTotal)

Dim Total As Decimal
Total = GetTotalPointer()
---------------------------------------------

Why use a delegate call instead of just saying Total =
ProductList.GetTotal() ?????

Thanks for any explanations

-Jason
 
OpticTygre said:
Ok, so I've been trying and trying to learn about delegates, but I still
can't grasp the concept of the advantages. Why use delegate functions
instead of calling the function directly?

In other words, if I had something like:

---------------------------------------------
Public Class ProductList

Public Function GetTotal() as Decimal
'Code omitted
End Function

End Class
---------------------------------------------

And then something like:

---------------------------------------------
Public Delegate Function GetTotalDelegate() as Decimal

Dim List As New ProductList()

Dim GetTotalPointer As New GetTotalDelegate(AddressOf List.GetTotal)

Dim Total As Decimal
Total = GetTotalPointer()
---------------------------------------------

Why use a delegate call instead of just saying Total =
ProductList.GetTotal() ?????

Thanks for any explanations

Indeed in this example there is no reason for the use of a delegate.
Delegates come in useful in more generic situations, where you *don't
know* what you are calling. If you like, you can consider a Delegate
type as an ultra-small Interface that consists of a single call. Like
an Interface, a Delegate type defines a *contract of behaviour*.

The usual place Delegates are found is in event handling. Suppose I
create a Phone class that raises an IncomingCall event. I want clients
of mine to be able to react to that event, so there needs to be some
defined mechanism by which clients can attach themselves to me.

What happens is that I define a Delegate type for that event: In
effect, I say to potential clients, "If you want to consume the
IncomingCall event, you must provide me with a function that accepts an
IncomingCallEventArgs and returns an Integer". That function signature
in effect *is* the Delegate type.

When clients pass me functions that match that signature, I have no
idea what those functions are, except that they match the signature.
They could be global module functions, shared class functions, class
instance methods - anything. All I *need* to know is that they accept
an IncomingCallEventArgs, and return me an Integer.


Thus we see that as with the concept of an Interface, the Delegate
provides a way to *encapsulate* behaviour and create *loosely-coupled*
components. *Contract-based programming* is another key phrase to throw
in there :)
 
Sometimes your code needs what in classic C language is called "a pointer to
a function". The typical example in C: a library provides a reusable quick
sort function which implements an ultra fast sorting algorithm to sort a
list, but this wonderful function does not know how to compare 2 items of
the list (because maybe they are not numbers), so it needs to receive a
pointer to a function provided by the developer to call when comparing 2
items. The comparision function must have a signature which receives 2
objects and return an integer with the comparision result (+1, 0,-1).

A .NET delegate is class (a type, then) which encapsulates a pointer to a
function to make it safe (managed), because the signature is defined in the
delegate. This means that while in C if you pass a pointer to function with
other signature or types different than expected you get a nice crash at
run-time, in NET the compiler detect at compile-time that the function
pointer will run fine.

There are many other scenarios where delegates can be used (events, etc).

--

Carlos J. Quintero

MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
You can code, design and document much faster.
http://www.mztools.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top