Relaxed delegates, one feature among the slew of new stuff coming in “Orcas” (VB 9.0 and .NET 3.5), provide more options for binding event handlers to events—even if the method/handler signatures don’t match exactly. Some relaxation of method binding was introduced in VB 8.0 and .NET 2.0, and an even greater relaxation of delegate signatures will be shipped with VB 9.0.
Many advanced design patterns depend on delegates. In fact, delegates actually are an implementation of the Observer behavior pattern. Completely understanding delegates will help you write more interesting and powerful code, in addition to just binding handlers to controls.
This article explains how delegates were relaxed in VB 8.0 and .NET 2.0, and explores the additional changes that will ship with Orcas (VB 9.0). It uses VB 8.0, the Orcas March 2007 CTP (Community Technology Preview), and Visual Studio 2005.
Reviewing Delegates and Binding
Delegates are implicitly instances of classes that contain methods that act as event handlers. You can bind event handlers to controls in the following ways:
- The old standard method of clicking on a control
- Selecting the event from the Events tab of the Property window
- Declaring an object with the WithEvents keyword and using the Handles object.eventname clause on the event method
- Using the AddHandler object.event, AddressOf eventhandler statement
You can conversely unbind event handlers by using the RemoveHandler object.event, AddressOf eventhandler statement.
Understanding Relaxed Delegates in VB 8.0
Prior to VB 8.0 and .NET 2.0, a delegate’s signature had to match the signature of the delegate type exactly. For example, a button’s Click event handler had to have two arguments: Object and EventArgs. The VB implementers eventually figured out that programmers often ignored these arguments. For example, in a Click event handler, they often ignore the object argument, which is initialized with the Button and the EventArgs. The EventArgs contain no useful information for Button clicks.
Thus, the VB team relaxed delegates in VB 8.0 to permit developers to use arguments that were subtypes of the actual type needed. For example, the following code is a valid handler for a Button’s Click event, even though the second argument is an Object type instead of an EventArgs type:
Sub HandlesClickToo(ByVal sender As Object, _ ByVal args As Object) Handles Button1.Click MessageBox.Show("Relaxed Handler") End Sub
This works because Object is a subtype of EventArgs. (In fact, Object is the ultimate subtype in .NET.)
Using Relaxed Delegates in VB 9.0
In VB 9.0, you won’t even have to provide arguments for the delegate. Relaxation has been extended to the point that the previous code can be revised as follows:
Sub HandlesClickToo Handles Button1.Click MessageBox.Show("Relaxed Handler") End Sub
Note: If you downloaded the May 2006 Orcas CTP, the compiler is still version 8.x and the above code will not compile.
Notice that there are no arguments at all. The basic idea is: ‘If event arguments are not used, why require them at all?’ Whether relaxed delegates will make coding easier remains to be seen.
Relaxed Delegates, Simpler Binding
Programmers often ignore delegate arguments and write the event handling code right against the known object itself. The premise behind relaxed delegates is that you will no longer be required to match the delegate argument types or even specify arguments to bind delegates to events. It’s subtle perhaps, but also helpful.
About the Author
Paul Kimmel is the VB Today columnist for www.codeguru.com and has written several books on object-oriented programming and .NET. Check out his new book UML DeMystified from McGraw-Hill/Osborne. Paul is a software architect for Tri-State Hospital Supply Corporation. You may contact him for technology questions at email@example.com.
If you are interested in joining or sponsoring a .NET Users Group, check out www.glugnet.org.