JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Early and Late Binding

When you're using a variable of type Object, the processing of the expression it's in may be deferred until run time. Deferring processing this way is called late binding. This is in contrast to early binding, where Visual Basic knows what the type of an expression is going to be at compile time.

Late binding, on the other hand, allows Object variables to be used in a typeless way, because how the members are used is based on the runtime type of the value in the variable. In early binding, which is the norm, Visual Basic knows the type of objects it is working with-but if the object is late bound, it doesn't know the type until run time, which is useful, because you can use the code with different objects. In polymorphism, you specify the class of a variable before you use it, so Visual Basic at least knows what kind of class members will be available. (Keep in mind that if you use a variable of a base class and assign a derived class object to it, you can't use members only defined in the derived class.) Here, Visual Basic has no idea what kind of object will be used; it's all done at run time.

Tip 

If you use Option Strict, late binding will cause an error.

You can see this in the LateBinding example on the CD-ROM. In this example, I create Animal and Fish classes, as we have before:

Public Class Animal
    Public Sub Breathe()
        MsgBox("Breathing...")
    End Sub
End Class

Public Class Fish
    Public Sub Breathe()
        MsgBox("Bubbling...")
    End Sub
End Class

Now I can pass objects of these classes to a Sub procedure named Display, treating the passed argument as an object of class Object. In Display, I'll call the passed object's Breathe method. Because Visual Basic will wait until run time to check if the pass object actually has a Breathe method, it can't tell you at design time if you're doing anything wrong, so I'll add a little exception handling just in case the passed object doesn't have a Breathe method:

    Private Sub Display(ByVal o As Object)
        Try
            o.Breathe()
        Catch
            MsgBox("Sorry, no Breathe method available.")
        End Try
    End Sub

Thanks to late binding, I can pass any kind of object to Display, and if that object has a Breathe method, that method will be invoked:

Public Class Form1
    Inherits System.Windows.Forms.Form

    'Windows Form Designer generated code

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
        Dim animal As New Animal()
        Dim jaws As New Fish()
        Display(animal)
        Display(jaws)
    End Sub

    Private Sub Display(ByVal o As Object)
        Try
            o.Breathe()
        Catch
            MsgBox("Sorry, no Breathe method available.")
        End Try
    End Sub
End Class

And that's how late binding works; I can use Display with any kind of object, not just those derived from some specific base class. (This actually works because all classes are derived from Object in Visual Basic.)

Now it's time to start looking into some of the more detailed aspects of what we've been discussing, and I'll do that in the Immediate Solutions section of this chapter.

Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor