(NET) NET (2005)

Events/Delegates in VB.NET
(Static/Dynamic declaration, AddressOf/RaiseEvent/AddHandler/WithEvents/Handles statement,
SingleCast/MultiCast/InvocationList, Invoke/BeginInvoke/DynamicInvoke/AsyncInvoke, Automatically instantiate.

   1:   
   2:  Module Module1
   3:   
   4:      'Define a Multicast delegate reference type that takes two integer
   5:      Public Delegate Sub CalculateTwoValuesDelegate(ByVal x As Integer, ByVal y As Integer)
   6:   
   7:      Dim WithEvents MyClassInstance2 As MyMethodClass
   8:   
   9:      Sub Main()
  10:   
  11:          Console.WriteLine("Static declaration")
  12:          MyClassInstance2 = New MyMethodClass()
  13:          MyClassInstance2.AddValues(5, 2)
  14:          MyClassInstance2.SubtractValues(5, 2)
  15:          MyClassInstance2.MultiplyValues(5, 2)
  16:   
  17:          Console.WriteLine()
  18:          Console.WriteLine("Dymanic declaration")
  19:   
  20:          'Three version of Adding Handler
  21:          '-------------------------------
  22:   
  23:          'Define an instance of the class MyMethodClass
  24:          Dim MyClassInstance As MyMethodClass = New MyMethodClass()
  25:   
  26:          '------
  27:          Console.WriteLine()
  28:          Console.WriteLine("AddHandler based on AddressOF")
  29:          AddHandler MyClassInstance.AddCalculateRequest, AddressOf MyClassInstance.Print
  30:          MyClassInstance.AddValues(6, 2)
  31:   
  32:          '------
  33:          Console.WriteLine()
  34:          Console.WriteLine("AddHandler based on Lambda expression")
  35:          AddHandler MyClassInstance.SubtractCalculateRequest, Sub(x, y) Console.WriteLine("x={0:N0},Y={1:N0}", x, y)
  36:          MyClassInstance.SubtractValues(6, 2)
  37:   
  38:          '------
  39:          Console.WriteLine()
  40:          Console.WriteLine("AddHandler based on delegate definition")
  41:          Dim MultiplyEventHandler As MyMethodClass.CalculateRequestEventDelegate = AddressOf MyClassInstance.MultiplyValues
  42:          AddHandler MyClassInstance.MultiplyCalculateRequest, AddressOf MyClassInstance.Print
  43:          MyClassInstance.MultiplyValues(6, 2)
  44:   
  45:          '------
  46:          Console.WriteLine()
  47:          Console.WriteLine("Create Instance of the delegate from my class - three separate singlecast delegate")
  48:          Dim AddInstDelegate As CalculateTwoValuesDelegate = AddressOf MyClassInstance.AddValues 'Automatically create an instance
  49:          Dim SubtractInstDelegate As CalculateTwoValuesDelegate = New CalculateTwoValuesDelegate(AddressOf MyClassInstance.SubtractValues)
  50:          Dim MultiplyInstDelegate As CalculateTwoValuesDelegate = New CalculateTwoValuesDelegate(AddressOf MyClassInstance.MultiplyValues)
  51:   
  52:          AddInstDelegate(7, 2)
  53:          SubtractInstDelegate(7, 2)
  54:          MultiplyInstDelegate(7, 2)
  55:   
  56:          '------
  57:          Console.WriteLine()
  58:          Console.WriteLine("Async Invoke AddValues by delegate in another thread")
  59:          Dim Res As IAsyncResult = AddInstDelegate.BeginInvoke(8, 2, AddressOf DelegateStartCallback, New With {.X = "AddInstDelegate"})
  60:          AddInstDelegate.EndInvoke(Res)
  61:          SubtractInstDelegate.BeginInvoke(8, 2, AddressOf DelegateStartCallback, New With {.X = "SubtractInstDelegate"})
  62:          MultiplyInstDelegate.BeginInvoke(8, 2, AddressOf DelegateStartCallback, New With {.X = "MultiplyInstDelegate"})
  63:   
  64:          '------
  65:          Console.WriteLine()
  66:          Console.WriteLine("Pass Delegates as parameter")
  67:          MySub(AddInstDelegate)
  68:          MySub(SubtractInstDelegate)
  69:          MySub(MultiplyInstDelegate)
  70:   
  71:          'VB.NET compiler will automatically instantiate a new delegate object for you, if necessary, when you use the AddressOf function.
  72:          'Three way of define delegates
  73:   
  74:          '------
  75:          Console.WriteLine()
  76:          Console.WriteLine("Full version of create instance of delegate")
  77:          Dim MulticastDelegate1 As CalculateTwoValuesDelegate = New CalculateTwoValuesDelegate(AddressOf MyClassInstance.AddValues)
  78:          'Create a new multicast Delegate using list of array of delegates
  79:          MulticastDelegate1 = CType([Delegate].Combine({New CalculateTwoValuesDelegate(AddressOf MyClassInstance.AddValues),
  80:                                                         New CalculateTwoValuesDelegate(AddressOf MyClassInstance.SubtractValues),
  81:                                                         New CalculateTwoValuesDelegate(AddressOf MyClassInstance.MultiplyValues)}
  82:                                                        ), CalculateTwoValuesDelegate)
  83:          MulticastDelegate1(10, 2)
  84:   
  85:          '
  86:          '------
  87:          Console.WriteLine()
  88:          Console.WriteLine("Alternative decraration instance of the delegate reference type (singlecast delegate)")
  89:          Dim MulticastDelegate2 As CalculateTwoValuesDelegate = AddressOf MyClassInstance.AddValues
  90:   
  91:          MulticastDelegate2 = CType([Delegate].Combine({New CalculateTwoValuesDelegate(AddressOf MyClassInstance.AddValues),
  92:                                                         New CalculateTwoValuesDelegate(AddressOf MyClassInstance.SubtractValues),
  93:                                                         New CalculateTwoValuesDelegate(AddressOf MyClassInstance.MultiplyValues)}
  94:                                                        ), CalculateTwoValuesDelegate)
  95:          MulticastDelegate2(11, 2)
  96:   
  97:          '
  98:          '------
  99:          Console.WriteLine()
 100:          Console.WriteLine("Automatily create an instance new multicast delegate using list of array of delegates")
 101:          Dim MulticastDelegate3 = CType([Delegate].Combine({New CalculateTwoValuesDelegate(AddressOf MyClassInstance.AddValues),
 102:                                                             New CalculateTwoValuesDelegate(AddressOf MyClassInstance.SubtractValues),
 103:                                                             New CalculateTwoValuesDelegate(AddressOf MyClassInstance.MultiplyValues)}
 104:                                                            ), CalculateTwoValuesDelegate)
 105:          MulticastDelegate3(12, 2)
 106:   
 107:          '
 108:          '------
 109:          Console.WriteLine()
 110:          Console.WriteLine("Dynamic invoke delegate each Delegate in InvocationList ")
 111:          Dim MulticastDelegate4 = CType([Delegate].Combine({New CalculateTwoValuesDelegate(AddressOf MyClassInstance.AddValues),
 112:                                                             New CalculateTwoValuesDelegate(AddressOf MyClassInstance.SubtractValues),
 113:                                                             New CalculateTwoValuesDelegate(AddressOf MyClassInstance.MultiplyValues)}
 114:                                                            ), CalculateTwoValuesDelegate)
 115:          Dim DelegateAsyncState = New Object
 116:          For Each OneDelegate As [Delegate] In MulticastDelegate4.GetInvocationList
 117:              OneDelegate.DynamicInvoke(13, 2)
 118:          Next
 119:   
 120:          '
 121:          '------
 122:          Console.WriteLine()
 123:          Console.WriteLine("Delegate Methods")
 124:          Dim DelegateMethods As Reflection.MethodInfo() = GetType(MyMethodClass.SubtractCalculateRequestEventHandler).GetMethods
 125:          For Each One As Reflection.MethodInfo In DelegateMethods
 126:              Console.WriteLine(One.Name)
 127:          Next
 128:          '
 129:          '
 130:          Console.WriteLine("Press any key to continue")
 131:          Console.ReadKey()
 132:      End Sub
 133:   
 134:      'pass delegate as parameters
 135:      Sub MySub(EventHandler As CalculateTwoValuesDelegate)
 136:          'invoke delegate
 137:          EventHandler.Invoke(9, 2)
 138:      End Sub
 139:   
 140:   
 141:      Sub MySub1(ByVal x As Integer, ByVal y As Integer) Handles MyClassInstance2.AddCalculateRequest, MyClassInstance2.MultiplyCalculateRequest, MyClassInstance2.SubtractCalculateRequest
 142:          Console.WriteLine("x={0:N0},Y={1:N0}", x, y)
 143:      End Sub
 144:   
 145:      Sub DelegateStartCallback(X As IAsyncResult)
 146:          Console.WriteLine(X.AsyncState.X & ", CurrentThread=" & Threading.Thread.CurrentThread.ManagedThreadId)
 147:      End Sub
 148:   
 149:   
 150:  End Module
 151:   
 152:   
 153:  'Events are essentially an accessor wrapper around a private delegate field, in the same way that properties often wrap a private field. 
 154:  'The big differences between Delegate fields And events are that events support the AddHandler And EventHandler functions 
 155:  'And events cannot be raised/invoked from outside Of the Class that defines it.
 156:   
 157:  Public Class MyMethodClass
 158:   
 159:      'Define Event without delegates (alternative decraration) - Delegate creates by compiler
 160:      Public Event AddCalculateRequest(ByVal x As Integer, ByVal y As Integer)
 161:   
 162:      'Event statement implicitly defines a delegate class named SubtractCalculateRequestEventHandler
 163:      Public Event SubtractCalculateRequest(ByVal x As Integer, ByVal y As Integer)
 164:   
 165:      'Manually Define a delegate 
 166:      Public Delegate Sub CalculateRequestEventDelegate(ByVal x As Integer, ByVal y As Integer)
 167:      'Define Event, based on delegate declaration
 168:      Public Event MultiplyCalculateRequest As CalculateRequestEventDelegate
 169:   
 170:   
 171:      Public Sub AddValues(ByVal x As Integer, ByVal y As Integer)
 172:          'First method of flash event
 173:          RaiseEvent AddCalculateRequest(x, y)
 174:          Console.Write("The sum of two values = ")
 175:          Console.WriteLine(x + y)
 176:      End Sub
 177:   
 178:      Public Sub SubtractValues(ByVal x As Integer, ByVal y As Integer)
 179:          'Second method of flash event - as method with suffix Event
 180:          SubtractCalculateRequestEvent(x, y)
 181:          Console.Write("The subtraction of two values = ")
 182:          Console.WriteLine(x - y)
 183:      End Sub
 184:   
 185:      Public Sub MultiplyValues(ByVal x As Integer, ByVal y As Integer)
 186:          RaiseEvent MultiplyCalculateRequest(x, y)
 187:          Console.Write("The multiplication of two values = ")
 188:          Console.WriteLine(x * y)
 189:      End Sub
 190:   
 191:      Public Sub Print(ByVal x As Integer, ByVal y As Integer)
 192:          Console.WriteLine("x={0:N0},Y={1:N0}", x, y)
 193:      End Sub
 194:  End Class
 195:   




Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23
Link to this page: //www.vb-net.com/Delegates/Index.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>