BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News VB Tips and Trips: Multiple Dispatch

VB Tips and Trips: Multiple Dispatch

With the plans for more dynamic programming in VBx, this is a good time to point out some of the dynamic features already available in Visual Basic. In this installment we talk about multiple dispatch.

Multiple dispatch, also known as multi-methods, is a technique related to function overloading. The key difference is timing. When overloaded functions are used, the compiler determines which one to call at compile time. Consider the below code:

Option Strict On
Module Module1 Sub Main() Dim obj As A
If Console.ReadKey.KeyChar = "A" Then obj = New A Else obj = New B End If Console.WriteLine(Foo(obj)) Console.ReadLine() End Sub Function Foo(ByVal value As A) As String Return "Function A Object Type " + value.RealName() End Function Function Foo(ByVal value As B) As String Return "Function B Object Type " + value.RealName() End Function End Module Class A Public Overridable Function RealName() As String Return "A" End Function End Class Class B Inherits A Public Overrides Function RealName() As String Return "B" End Function End Class

When you run this code, you will either see "Function A Object Type A" or "Function A Object Type B". Even though obj points to an object of type B, the compiler has already decided that the first version of Foo will be called. This is sometimes referred to as single dispatch and is often a stumbling block for novice programmers.

If you make a subtle change shown, you change the timing so that the decision is put off until runtime.

Option Strict Off 'Change 1 - Late binding is enabled

    Sub Main()
        Dim obj 'Change 2 - obj is late bound
        If Console.ReadKey.KeyChar = "A" Then
            obj = New A
        Else
            obj = New B
        End If
        Console.WriteLine(Foo(obj))
        Console.ReadLine()

    End Sub

With this version, you will see "Function A Object Type A" or "Function B Object Type B". This ability to choose the correct function at runtime is what people refer to as dynamic dispatch.

Dynamic dispatch is more than just an interesting side effect of having overloads in a language that supports late binding. It is also a useful technique when dealing with homogonous lists.

Let's say you want to apply custom formatting to all controls on a form where each control type has its own formatting function. Examples would include FormatControl(TextBox) and FormatControl(ListBox).

Using a statically typed language like C#, you have to can call FormatControl on each control in the form's collection via a loop. But since the function is determined at compile time, you have to provide a generic function called FormatControl (control). This generic function then uses a giant if/else-if block to determine the real function to call.

By using dynamic dispatch, the runtime determines which version of FormatControl to call depending on the object in memory without the need for the if/else-if block. You would still need FormatControl (control), but it would be an empty function for catching miscellaneous controls.

Caution has to be used when relying on dynamic dispatch. Since the decision is put off until runtime, it is possible to get an exception if an appropriate overload cannot be found. This isn't an issue in statically typed languages where you would get a compiler error instead.

 

Rate this Article

Adoption
Style

BT