Declaring events in a class built in VB.NET is done using Event specification. Defining event involves assigning the name and arguments providing.
Because an event is included in a class, the objects defined on that class may send messages related to event – raise. Transmission is accomplished by specification RaiseEvent. Assigning the event to procedure for event handling is done by specifications Handles or AddHandler […].
Event message is sent to the entity in which it was defined. For example, a derived class cannot exchange messages related to events inherited from base class.
Events have not return type, but may have the list of arguments.
'creating a class containing an event,_ event handler de and procedure for event-event handler association Class GrupOp 'defining event handler Public Sub OpDiferenta(ByVal a As Integer, ByVal b As Integer) Dim dif As Integer dif = a - b MsgBox("Substraction value is: " & CStr(dif)) End Sub 'defining an event Public Event Calcul(ByVal x As Integer, ByVal y As Integer) 'defining procedure for event-event handler association Public Sub SetUpEventHanlderDif(ByVal a As Integer,_ ByVal b As Integer) AddHandler Calcul, AddressOf OpDiferenta End Sub End Class
In the above example, the event and the two methods Sub are defined under the same class. Thus, specification AddHandler has not to cover the event and the event handler on object variables defined as GrupOp type.
Method OpDiferenta is the event handler. The association of event Calcul and event handler OpDiferenta is done by the method SetUpEventHandlerDif.