Home All Groups Group Topic Archive Search About

?Delegates and Interaces - similarities/differences?

Author
12 Jul 2006 9:00 PM
Rich
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

Author
13 Jul 2006 8:31 AM
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).

--
Patrice

"Rich" <R***@discussions.microsoft.com> a écrit dans le message de news:
F617F4E4-8469-4889-B7DC-78CE6A070***@microsoft.com...
Show quoteHide quote
> 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
>
Author
13 Jul 2006 3:25 PM
Rich
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

Show quoteHide quote
"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" <R***@discussions.microsoft.com> a écrit dans le message de news:
> F617F4E4-8469-4889-B7DC-78CE6A070***@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
> >
>
>
>
Author
13 Jul 2006 10:09 PM
Branco Medeiros
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.