473,396 Members | 2,037 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,396 software developers and data experts.

Help with delegates

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
Nov 21 '05 #1
2 1179

OpticTygre wrote:
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 :)
--
Larry Lard
Replies to group please

Nov 21 '05 #2
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

"OpticTygre" <op********@adelphia.net> escribió en el mensaje
news:kL********************@adelphia.com...
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

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nicky Smith | last post by:
Hello, I'm reading Mike Gunderloy's Mcad Vb.net book, and I've also read the MS press Mcad book for the same topic ".. Windows based applications with VB.net" for exam 70-306. In the...
4
by: AMDRIT | last post by:
I am trying to understand Delegates and where/when to use them. I can see one potential use of a delegate (on form closing, set the cancel property in the event arguments.) Does anyone have a...
0
by: Siegfried Heintze | last post by:
I'm practicing for the C# brain bench test by reviewing how delegates work. (Delegates are easy to to with visual studio because you normally use the delegates that some API as already defined. I'm...
13
by: Praveen | last post by:
trying to learn plymorphism. My sample is public class Class1 { public static void Main(string args) { Cls1 x = new Cls1(); Cls2 y = new Cls2(); Cls3 y = new Cls3();
5
by: sajin | last post by:
Hi All.. We are using VB .Net 2005 for implementing an API. API needs to generate events. For this client wants us to use Windows Callback (delegate implementation). The intention of using...
0
by: bharathreddy | last post by:
Delegates Here in this article I will explain about delegates in brief. Some important points about delegates. This article is meant to only those who already know delegates, it will be a quick...
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...
69
by: raylopez99 | last post by:
They usually don't teach you in most textbooks I've seen that delegates can be used to call class methods from classes that are 'unaware' of the delegate, so long as the class has the same...
4
by: Miro | last post by:
I am trying to understand delegates and I think I do understand them ... just hoping if someone can tell me im on the right track. So far what I have read is that a Delegate is an Asynchronus call...
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.