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

  • Lenovo recommends Windows 8 Pro. "I dropped my laptop getting out of the taxi." This probably sounds familiar to most IT professionals. If your employees are traveling, you know their devices are in for a rough go. Whether it's a trip to the conference room or a convention out of town, any time equipment leaves a user's desk it is at risk of being put into harm's way. Stay connected at all times, whether at the office or on the go, with agile, durable, and flexible devices like the Lenovo® …

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date