473,395 Members | 1,539 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,395 software developers and data experts.

?Delegates and Interaces - similarities/differences?

Hello,

I recently started using delegates in my VB apps to handle asynchronous
operations. Based on the examples that I followed, delegates appear to work
in a similar way as Interfaces. Interfaces define methods and functions
that a class which implements the interface must also contain - so I get the
impression that an Interface is like a skeleton of the methods... And a
delegate defines the arguments for a function which have to be followed/used
by the functions/methods which are called by events assigned to the delegate.

So my simplistic question is if I can think of a delegate as a sort of
interface for functions and methods in a class where the delegate is assigned
to events in the class that call these functions/methods?

Thanks,
Rich

Jul 12 '06 #1
3 1149
An interface defines the whole set of methods that are available for classes
implementing this interface (for example if you implement IComparable in
your class, then other .NET classes will be able to compare two instances of
your class making for example possible to use Array.Sort to sort an array of
instances).

A delegate is a (typed) function pointer allowing to make sure that you can
call throught it anything having the same signature (for example it could be
a way to perform a calculation using a user selectable algorithm. Instead of
testing each time the selected algorithm to call the relevant method, you
could just test once to initialize the function pointer. Then the function
pointer (delegate) will allow to always call the selected algorithm in your
whole code).

--
Patrice

"Rich" <Ri**@discussions.microsoft.coma écrit dans le message de news:
F6**********************************@microsoft.com...
Hello,

I recently started using delegates in my VB apps to handle asynchronous
operations. Based on the examples that I followed, delegates appear to
work
in a similar way as Interfaces. Interfaces define methods and functions
that a class which implements the interface must also contain - so I get
the
impression that an Interface is like a skeleton of the methods... And a
delegate defines the arguments for a function which have to be
followed/used
by the functions/methods which are called by events assigned to the
delegate.

So my simplistic question is if I can think of a delegate as a sort of
interface for functions and methods in a class where the delegate is
assigned
to events in the class that call these functions/methods?

Thanks,
Rich

Jul 13 '06 #2
Thank you for this explanation. Now I will have to spend time digesting it.
The last time I worked with a function pointer directly (consciensciously) -
where I actually dealt with the value/address of a function was in a C++
class years ago.

Rich

"Patrice" wrote:
An interface defines the whole set of methods that are available for classes
implementing this interface (for example if you implement IComparable in
your class, then other .NET classes will be able to compare two instances of
your class making for example possible to use Array.Sort to sort an array of
instances).

A delegate is a (typed) function pointer allowing to make sure that you can
call throught it anything having the same signature (for example it could be
a way to perform a calculation using a user selectable algorithm. Instead of
testing each time the selected algorithm to call the relevant method, you
could just test once to initialize the function pointer. Then the function
pointer (delegate) will allow to always call the selected algorithm in your
whole code).

--
Patrice

"Rich" <Ri**@discussions.microsoft.coma écrit dans le message de news:
F6**********************************@microsoft.com...
Hello,

I recently started using delegates in my VB apps to handle asynchronous
operations. Based on the examples that I followed, delegates appear to
work
in a similar way as Interfaces. Interfaces define methods and functions
that a class which implements the interface must also contain - so I get
the
impression that an Interface is like a skeleton of the methods... And a
delegate defines the arguments for a function which have to be
followed/used
by the functions/methods which are called by events assigned to the
delegate.

So my simplistic question is if I can think of a delegate as a sort of
interface for functions and methods in a class where the delegate is
assigned
to events in the class that call these functions/methods?

Thanks,
Rich


Jul 13 '06 #3
Rich wrote:
I recently started using delegates in my VB apps to handle asynchronous
operations. Based on the examples that I followed, delegates appear to work
in a similar way as Interfaces. Interfaces define methods and functions
that a class which implements the interface must also contain - so I get the
impression that an Interface is like a skeleton of the methods... And a
delegate defines the arguments for a function which have to be followed/used
by the functions/methods which are called by events assigned to the delegate.
<snip>

The declaration of a delegate defines a method signature and, under the
curtains, defines a new class that inherits from System.Delegate. This
class is capable of storing references to methods, just as if they were
function pointers, as long as the method signature matches the one
specified upon the class declaration.

When you declare a delegate, you're actually specifying the signature
of its Invoke method:

Delegate Function MyDelegate(ByVal SomeValue As Integer) As String

the declaration above is conceptually similar to having a class
declaration like this (only that it's forbidden by the framework to
inherit from System.Delegate: only compilers can do so):

Class MyDelegate
Inherits System.Delegate
Sub New(Source As Object, MethodName As String)
MyBase.New(Source, MethodName)
End Sub

Sub New(Source As Type, MethodName As String)
MyBase.New(Source, MethodName)
End Sub

Function Invoke(ByVal SomeValue As Integer) As String
'well, I'm just guessing, here:
Dim Result As Object = DynamicInvoke(SomeValue)
Return CType(Result, String)
End Function

End Class

Notice that the class constructor asks for the method name, not some
funky method pointer (because there aren't really function pointers
available in the managed environment).

So, when you instanciate a delegate:

Dim S As MyDelegate = AddressOf MyMethod

You're actually doing something like this, where the underlying type
system will locate the actual method named "MyMethod" whose signature
matches the one of the delegate's Invoke method.

Dim S As New MyDelegate(Me, "MyMethod")

Smoke and mirrors, as allways, but really nice mirrors, way cool
smoke... =)

HTH.

Have fun,

Branco.

Jul 13 '06 #4

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

Similar topics

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...
29
by: Brett | last post by:
I'd like to better understand how the following code works. I've posted questions below. namespace Something.Something1 { using System; public delegate void Test1(); public delegate void...
3
by: Jon Shemitz | last post by:
Advanced Delegates Delegates may be the least-understood part of the CLR. Delegates look a bit like method pointers, but the differences go well beyond delegates' multi-cast abilities. I will...
6
by: kalishandy | last post by:
I have the following code to tie up a custom delegate (ConsoleDelegate) with two methods from a separate class called CallMyEvents: static void Main() { CallMyEvents call = new CallMyEvents();...
4
by: dani kotlar | last post by:
I am writing a costum delegate and I wondere what is the difference between an event and a delegate instance? Why should I declare an event with the event keyword instead of declaring it as an...
5
vinayvajp
by: vinayvajp | last post by:
hi can anybody tell the similarities and dissimilarities between the Threads and Delegates and how they can be used together. Thanks in advance. vin rao
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...
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...
2
by: BobLewiston | last post by:
The concept of delegates (references to methods) per se is new to me (although I used function pointers in C years ago), and although the literature acknowledges that there are significant...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.