Public Delegate Function BinaryOp(ByVal x As Integer, ByVal y As Integer) As Integer
'this class defines the methods that will be "pointed to" by the delegate
Public Class SimpleMath
Public Shared Function Add(ByVal x As Integer, ByVal y As Integer) As Integer
Return x + y
End Function
Public Shared Function Subtract(ByVal x As Integer, ByVal y As Integer) As Integer
Return x - y
End Function
End Class
Module Module1
Sub DisplayDelegateInfo(ByVal delObj As System.Delegate)
For Each d As System.Delegate In delObj.GetInvocationList()
Console.WriteLine("Method Name: {0}", d.Method)
Console.WriteLine("Type name: {0}", d.Target)
Next
End Sub
Sub Main()
Console.WriteLine("***** Simple Delegate Example *****" & vbLf)
'make a delegate object and add a method to the invocation
'liust using the AddressOf keyword
Dim b As BinaryOp = New BinaryOp(AddressOf SimpleMath.Add)
'invoke the method 'pointed to'
Console.WriteLine("10 + 10 is {0}", b(10, 10))
DisplayDelegateInfo(b)
Console.WriteLine()
End Sub
End Module
Module Module1
Sub Main()
Console.WriteLine("***** Delegates as event enablers *****")
Dim c1 As New Car("SlugBug", 10)
'pass the address of the methods that will be maintained
'by the delegate member variables of the car type
c1.OnAboutToBlow(AddressOf CarAboutToBlow)
c1.OnExploded(AddressOf CarExploded)
Console.WriteLine("***** Speeding Up *****")
For i As Integer = 0 To 5
c1.Accelerate(20)
Next
Console.ReadLine()
End Sub
Public Sub CarAboutToBlow(ByVal msg As String)
Console.WriteLine(msg)
End Sub
Public Sub CarExploded(ByVal msg As String)
Console.WriteLine(msg)
End Sub
End Module
Module Module1
'declare member variables "WithEvents" to capture the events
Private WithEvents c As New Car("NightRider", 50)
Sub Main()
Console.WriteLine("***** Fun with Events *****")
Dim i As Integer
For i = 0 To 5
c.Accelerate(10)
Next
Console.ReadLine()
End Sub
'event handlers
Public Sub MyExplodedHandler(ByVal s As String) Handles c.Exploded
Console.WriteLine(s)
End Sub
Public Sub MyAboutToDieHandler(ByVal s As String) Handles c.AboutToBlow
Console.WriteLine(s)
End Sub
End Module
Module Module1
Sub main()
Console.WriteLine("***** Fun with AddHandler/RemoveHandler *****")
'note the lack of WithEvents keyword
Dim c As New Car("NightRider", 50)
'dynamically hook into event using addhandler
AddHandler c.Exploded, AddressOf CarEventHandler
AddHandler c.AboutToBlow, AddressOf CarEventHandler
For I As Integer = 0 To 5
c.Accelerate(10)
Next
Console.ReadLine()
End Sub
Public Sub CarEventHandler(ByVal s As String)
Console.WriteLine(s)
End Sub
End Module
'this event has been customized
Public Custom Event EngineStart As System.EventHandler
AddHandler(ByVal value As System.EventHandler)
Console.WriteLine("Added connection")
arConnections.Add(value)
End AddHandler
RemoveHandler(ByVal value As System.EventHandler)
End RemoveHandler
RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
For Each h As EventHandler In arConnections
Console.WriteLine("Raising event")
h(sender, e)
Next
End RaiseEvent
End Event
Public Sub Start()
RaiseEvent EngineStart(Me, New EventArgs())
End Sub
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment