events and delegates ??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    events and delegates ??

    Hi,

    what is the difference between using events and delegates (apart from the
    syntax) ?

    have a look at following (working) programs please
    (you can just copy/paste and build it) :

    First program uses delegates, the second events but both do inherently the
    same :

    Using delegates :
    Imports System
    Public Delegate Sub WakeUpDelegate( )

    class AClass
    Public Shared Sub WakeUpA()
    Console.WriteLi ne("WakeUpA()" )
    End Sub
    End Class

    Class Timer
    Public deleg As WakeUpDelegate
    Public Sub Alarm()
    If Not deleg Is Nothing Then
    deleg()
    End If
    End Sub
    End Class

    Class Program
    Public Shared Sub Main()
    Console.WriteLi ne("Using Delegates (VB)")
    Dim objTimer As Timer = New Timer
    Dim callback As WakeUpDelegate = New WakeUpDelegate( AddressOf
    AClass.WakeUpA)
    objTimer.deleg = CType([Delegate].Combine(objTim er.deleg, callback),
    WakeUpDelegate)
    objTimer.Alarm( )
    End Sub
    End Class


    Using events :
    Imports System
    Public Delegate Sub WakeUpDelegate( )

    class AClass
    Public Shared Sub WakeUpA()
    Console.WriteLi ne("WakeUpA()" )
    End Sub
    End Class

    Class Timer
    Public Event deleg As WakeUpDelegate
    Public Sub Alarm()
    RaiseEvent deleg()
    End Sub
    End Class

    Class Program
    Public Shared Sub Main()
    Console.WriteLi ne("Using Events (VB)")
    Dim objTimer As Timer = New Timer
    AddHandler objTimer.deleg, AddressOf AClass.WakeUpA
    objTimer.Alarm( )
    End Sub
    End Class



    Thanks for any help
    Chris


  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: events and delegates ??

    Chris,
    The way I view it is:

    Events are implemented in terms of a Delegate, similar to how a Property is
    implemented in terms of a Field.

    The Event protects the underlying Delegate, similar to how a Property
    protects the underlying Field.

    In other words you use Events when you want to encapsulate the Delegate,
    similar to how you use a Property when you want to encapsulate a field.

    See:



    Hope this helps
    Jay

    "Chris" <christianc@pan dora.be> wrote in message
    news:Pd3zb.5648 7$BG5.1981470@p hobos.telenet-ops.be...[color=blue]
    > Hi,
    >
    > what is the difference between using events and delegates (apart from the
    > syntax) ?
    >
    > have a look at following (working) programs please
    > (you can just copy/paste and build it) :
    >
    > First program uses delegates, the second events but both do inherently the
    > same :
    >
    > Using delegates :
    > Imports System
    > Public Delegate Sub WakeUpDelegate( )
    >
    > class AClass
    > Public Shared Sub WakeUpA()
    > Console.WriteLi ne("WakeUpA()" )
    > End Sub
    > End Class
    >
    > Class Timer
    > Public deleg As WakeUpDelegate
    > Public Sub Alarm()
    > If Not deleg Is Nothing Then
    > deleg()
    > End If
    > End Sub
    > End Class
    >
    > Class Program
    > Public Shared Sub Main()
    > Console.WriteLi ne("Using Delegates (VB)")
    > Dim objTimer As Timer = New Timer
    > Dim callback As WakeUpDelegate = New WakeUpDelegate( AddressOf
    > AClass.WakeUpA)
    > objTimer.deleg = CType([Delegate].Combine(objTim er.deleg,[/color]
    callback),[color=blue]
    > WakeUpDelegate)
    > objTimer.Alarm( )
    > End Sub
    > End Class
    >
    >
    > Using events :
    > Imports System
    > Public Delegate Sub WakeUpDelegate( )
    >
    > class AClass
    > Public Shared Sub WakeUpA()
    > Console.WriteLi ne("WakeUpA()" )
    > End Sub
    > End Class
    >
    > Class Timer
    > Public Event deleg As WakeUpDelegate
    > Public Sub Alarm()
    > RaiseEvent deleg()
    > End Sub
    > End Class
    >
    > Class Program
    > Public Shared Sub Main()
    > Console.WriteLi ne("Using Events (VB)")
    > Dim objTimer As Timer = New Timer
    > AddHandler objTimer.deleg, AddressOf AClass.WakeUpA
    > objTimer.Alarm( )
    > End Sub
    > End Class
    >
    >
    >
    > Thanks for any help
    > Chris
    >
    >[/color]


    Comment

    • Armin Zingler

      #3
      Re: events and delegates ??

      "Chris" <christianc@pan dora.be> schrieb[color=blue]
      >
      > what is the difference between using events and delegates (apart from
      > the syntax) ?
      >
      > have a look at following (working) programs please
      > (you can just copy/paste and build it) :
      >
      > First program uses delegates, the second events but both do
      > inherently the same :
      > [sample code][/color]


      The difference is that an event is a MulticastDelega te, i.e. raising the
      event can notify an unlimited number of objects about the event. Simplified,
      an event is an array of delegates (AFAIR, in fact it is a linked list).


      --
      Armin





      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: events and delegates ??

        Armin,[color=blue]
        > The difference is that an event is a MulticastDelega te,[/color]
        A delegate can be multicast also, remember MulticastDelega te inherits from
        Delegate. Although I agree with your statement in that most of the time a
        Delegate variable are single callbacks.

        Try the following to see what I mean.

        Private Delegate Sub Stuff()

        Public Sub Main
        Dim a, b, c As Stuff
        a = AddressOf Method1
        b = AddressOf Method2
        c = DirectCast([Delegate].Combine(a, b), Stuff)
        c.Invoke()
        End Sub

        Public Sub Method1()
        Debug.WriteLine ("Hello", "Method1")
        End Sub

        Public Sub Method2()
        Debug.WriteLine ("Goodbye", "Method2")
        End Sub

        Hope this helps
        Jay



        "Armin Zingler" <az.nospam@free net.de> wrote in message
        news:OXhmKbPuDH A.2308@TK2MSFTN GP11.phx.gbl...[color=blue]
        > "Chris" <christianc@pan dora.be> schrieb[color=green]
        > >
        > > what is the difference between using events and delegates (apart from
        > > the syntax) ?
        > >
        > > have a look at following (working) programs please
        > > (you can just copy/paste and build it) :
        > >
        > > First program uses delegates, the second events but both do
        > > inherently the same :
        > > [sample code][/color]
        >
        >
        > The difference is that an event is a MulticastDelega te, i.e. raising the
        > event can notify an unlimited number of objects about the event.[/color]
        Simplified,[color=blue]
        > an event is an array of delegates (AFAIR, in fact it is a linked list).
        >
        >
        > --
        > Armin
        >
        > http://www.plig.net/nnq/nquote.html
        > http://www.netmeister.org/news/learn2quote.html
        >
        >[/color]


        Comment

        Working...