?Delegates and Interaces - similarities/differences?

G

Guest

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
 
P

Patrice

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).
 
G

Guest

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
 

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

Top