Cerebrus

Events and Delegates

Aug 6th, 2009
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.68 KB | None | 0 0
  1. 'Related to question at:
  2. 'http://groups.google.com/group/dotnetdevelopment/browse_thread/thread/5636b78c34487de4
  3.  
  4. 'This is all about understanding Events and Delegates.
  5. 'To illustrate the concept, I created a sample class, WiaClass:
  6.  
  7. '---
  8. Public Class WiaClass
  9.   'Let VB generate the Delegates automatically.
  10.   Public Event OnDeviceDisconnected(ByVal sender As Object, ByVal e As EventArgs)
  11.   Public Event OnTransferComplete(ByVal sender As Object, ByVal e As EventArgs)
  12.  
  13.   Public Sub TransferAllFunds()
  14.     RaiseEvent OnTransferComplete(Nothing, Nothing)
  15.     Disconnect()
  16.   End Sub
  17.  
  18.   Private Sub Disconnect()
  19.     RaiseEvent OnDeviceDisconnected(Nothing, Nothing)
  20.   End Sub
  21. End Class
  22. '---
  23.  
  24. 'Calling code subscribes to this as follows:
  25. '---
  26. Public Class MyWindowsForm
  27.   Private Sub CreateManager()
  28.     Dim wiaMgr As New WiaClass()
  29.     AddHandler wiaMgr.OnDeviceDisconnected, AddressOf OnDeviceDisconnectedHandler
  30.     AddHandler wiaMgr.OnTransferComplete, AddressOf OnTransferCompleteHandler
  31.  
  32.     'Call the method that raises the events.
  33.     wiaMgr.TransferAllFunds()
  34.   End Sub
  35.  
  36.   Private Sub OnDeviceDisconnectedHandler(ByVal sender As Object, ByVal e As EventArgs)
  37.     MessageBox.Show("Device disconnected!!")
  38.   End Sub
  39.  
  40.   Private Sub OnTransferCompleteHandler(ByVal sender As Object, ByVal e As EventArgs)
  41.     MessageBox.Show("Transfer Completed!!")
  42.   End Sub
  43. End Class
  44. '---
  45.  
  46. 'When more handlers are needed for the same Event type, we can declare a Delegate type as follows:
  47.  
  48. '---
  49. Public Class WiaClass
  50.   Public Delegate Sub _IWiaEvents_OnDeviceDisconnectedEventHandler(ByVal sender As Object, ByVal e As EventArgs)
  51.   Public Delegate Sub _IWiaEvents_OnTransferCompleteEventHandler(ByVal sender As Object, ByVal e As EventArgs)
  52.   Public Event OnDeviceDisconnected As _IWiaEvents_OnDeviceDisconnectedEventHandler
  53.   Public Event OnTransferComplete As _IWiaEvents_OnTransferCompleteEventHandler
  54.  
  55.   Public Sub TransferAllFunds()
  56.     RaiseEvent OnTransferComplete(Nothing, Nothing)
  57.     Disconnect()
  58.   End Sub
  59.  
  60.   Private Sub Disconnect()
  61.     RaiseEvent OnDeviceDisconnected(Nothing, Nothing)
  62.   End Sub
  63.  
  64. End Class
  65. '---
  66.  
  67. 'Calling code can access it as before or in the following way (which is similar to the code you require converted to VB) :
  68. '---
  69. Private Sub CreateManager()
  70.   Dim wiaMgr As New WiaClass()
  71.   AddHandler wiaMgr.OnDeviceDisconnected, New WiaClass._IWiaEvents_OnDeviceDisconnectedEventHandler(AddressOf wia_OnDeviceDisconnectedHandler)
  72.   AddHandler wiaMgr.OnTransferComplete, New WiaClass._IWiaEvents_OnTransferCompleteEventHandler(AddressOf wia_OnTransferCompleteHandler)
  73.  
  74.   'Call the method that raises the events.
  75.   wiaMgr.TransferAllFunds()
  76. End Sub
  77. '---
Advertisement
Add Comment
Please, Sign In to add comment