C
Chris
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.WriteLine("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.WriteLine("Using Delegates (VB)")
Dim objTimer As Timer = New Timer
Dim callback As WakeUpDelegate = New WakeUpDelegate(AddressOf
AClass.WakeUpA)
objTimer.deleg = CType([Delegate].Combine(objTimer.deleg, callback),
WakeUpDelegate)
objTimer.Alarm()
End Sub
End Class
Using events :
Imports System
Public Delegate Sub WakeUpDelegate()
class AClass
Public Shared Sub WakeUpA()
Console.WriteLine("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.WriteLine("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
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.WriteLine("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.WriteLine("Using Delegates (VB)")
Dim objTimer As Timer = New Timer
Dim callback As WakeUpDelegate = New WakeUpDelegate(AddressOf
AClass.WakeUpA)
objTimer.deleg = CType([Delegate].Combine(objTimer.deleg, callback),
WakeUpDelegate)
objTimer.Alarm()
End Sub
End Class
Using events :
Imports System
Public Delegate Sub WakeUpDelegate()
class AClass
Public Shared Sub WakeUpA()
Console.WriteLine("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.WriteLine("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