0% found this document useful (0 votes)
2 views

complete-reference-vb_net_1

Uploaded by

khalid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

complete-reference-vb_net_1

Uploaded by

khalid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

The .

NET Framework Event Model: Delegates and Events


QSortDel3 = CType(System.Delegate.Combine(QSortDel1, QSortDel2), QSortDel1)
QSortDel3 'Invokes the method call from P1 and P2

End Sub

You can call the third Delegate's GetInvocationList method, which returns an array of references to you.
You can then inspect the array list of method references by simply iterating and displaying the contents of the
array to the console.

Delegates are immutable, so once you create them and populate the invocation list, you can't change it.
Changing invocation order can only be done by creating a new Delegate.

The Delegate class also contains methods for combining operations. In other words, you can take one
Delegate and combine its invocation list with that of another. This action, performed with the Combine
method, does not change the current Delegate. Instead, it returns a new Delegate with the combined
operations of the two original ones. The Remove method performs the opposite of Combine; it returns a new
Delegate with the invocation list of one of the Delegates removed. Combine returns null if one of the
Delegate's lists is devoid of method references. In this case, the Delegate is returned unchanged when the
combining or removing operation does not affect anything.

Delegates return values to their clients when the referenced method signature returns a value. For example,
when the method to invoke is a function, or a result type, the Delegate returns the value it receives from the
Delegatee or Receiver class.

Naturally, multicast methods cannot return values from multiple invocations of function methods; thus,
multicast Delegates are declared as Sub Delegates. However, when the signature of a method includes a
parameter that is passed by reference, its final value is the result of every method in the list executing
sequentially and updating the parameter's value.

Any one of the methods in either the unicast or multicast Delegate can throw an exception. When an
exception occurs, the method stops executing and the exception is passed back to the caller of the Delegate.
The remaining methods in the list are not invoked, even if you catch and successfully handle the exception.

Note .NET compilers provide two additional methods to the Delegate for asynchronous programming, and
callback operations: BeginInvoke and EndInvoke.

The .NET Framework Event Model: Delegates and Events


Using Delegates in an event model is also not a new idea. Windows−bound Java programmers encountered
them in the Windows Foundation Classes and used them to wire up events in applications created with Visual
J++.

Delegates are used to bridge the listeners for events (the Receiver objects we have been discussing) to the
events in the Sender object or client. As soon as something happens in the client the Delegate is invoked and
it calls the method it is pointing to in the Receiver object. That's all there is to this event model. Besides
several constructs that make wiring and plumbing the event system in your application easier, there is nothing
more to the .NET event model than what we have already covered in this chapter. But let's look at the event
constructs a little closer.

Suppose my Trajectory application needs to constantly monitor space for an asteroid threatening the ship and
suddenly one comes into a critical proximity. The software can fire an event that collects the necessary data

485

You might also like