As discussed in the In Depth section of this chapter, when the target of an expression is of type Object, the processing of the expression may be deferred until run time. Deferring processing this way is called late binding. In early binding, which is the norm, Visual Basic knows the type of an expression at compile time, but in late binding, the type of an expression isn't known until run time. As we saw in the In Depth section of this chapter, using the example called LateBinding on the CD-ROM, you can create a base class and a derived class this way:
Then you can pass objects of either of these classes to a method if you use an argument of class Object:
Private Sub Display(ByVal o As Object) Try o.Breathe() Catch MsgBox("Sorry, no Breathe method available.") End Try End Sub
Here's how this works in the LateBinding example:
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
For more on how this example works, take a look at the In Depth section of the chapter.