Understanding Relaxed Delegates in VB

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 pkimmel@softconcepts.com.

If you are interested in joining or sponsoring a .NET Users Group, check out www.glugnet.org.


  • Re: "This works because Object is a subtype of EventArgs. (In fact, Object is the ultimate subtype in .NET"

    Posted by cjard on 03/09/2007 11:29am

    Subtype?.. As in subclass? Object is a subtype of EventArgs means Object inherits from EventArgs
    When it comes to writing articles, nomenclature should follow the architecture on which the article is based.. I'd have preferred to see:
    "This works because Object is a base-type of EventArgs. (In fact, Object is the ultimate base in .NET)"
    To me, EventArgs is a subtype of Object. Infact, ignore what I think:
    Wikipedia thinks EventArgs is a subtype of Object too!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Hybrid cloud platforms need to think in terms of sweet spots when it comes to application platform interface (API) integration. Cloud Velocity has taken a unique approach to tight integration with the API sweet spot; enough to support the agility of physical and virtual apps, including multi-tier environments and databases, while reducing capital and operating costs. Read this case study to learn how a global-level Fortune 1000 company was able to deploy an entire 6+ TB Oracle eCommerce stack in Amazon Web …

  • "Security" is the number one issue holding business leaders back from the cloud. But does the reality match the perception? Keeping data close to home, on premises, makes business and IT leaders feel inherently more secure. But the truth is, cloud solutions can offer companies real, tangible security advantages. Before you assume that on-site is the only way to keep data safe, it's worth taking a comprehensive approach to evaluating risks. Doing so can lead to big benefits.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds