Early Versus Late Binding

84

2.14.12 Early Versus Late Binding

Declarations permit the compiler to know the data type of the item being declared. Here is the declaration of a variable of type String: Dim s As String Knowing the data type of a variable or parameter, field, etc. allows the compiler to determine what operations are permitted on any object referenced by the variable. For example, given the previous declaration of s as String, the compiler knows that the expression s.Trim is permitted because it is defined in the String class, while s.Compact is not because there is no such method in the String class. During compilation, the Visual Basic .NET compiler complains if it encounters such errors. There is, however, one case in which the developer is permitted to relax this constraint. If Option Strict is turned off, the compiler forgoes this kind of checking on variables of type Object. For example, the following code will compile without difficulty, even though the Object class doesnt have a method called Whatever: Option Strict Off ... Dim obj As Object obj.Whatever With Option Strict off, the compiler compiles obj.Whatever to code that checks to see if the runtime type of the object referenced by obj is a type that possesses a Whatever method. If it does, the Whatever method is called. If not, a runtime exception is raised. Here is such a scenario: Option Strict Off Public Class WhateverClass Public Sub Whatever Console.WriteLineWhatever End Sub End Class Public Class TestClass Public Shared Sub TestMethod Dim obj As Object obj = New WhateverClass obj.Whatever End Sub End Class Because Option Strict is off, this code compiles just fine. Because obj references an object at runtime that is of a class that implements a Whatever method, it also runs just fine. However, consider what happens if the Whatever method is removed from the WhateverClass class: Option Strict Off Public Class WhateverClass End Class Public Class TestClass Public Shared Sub TestMethod Dim obj As Object obj = New WhateverClass obj.Whatever 85 End Sub End Class The code still compiles without a problem, because Option Strict is off. However, at runtime there is a problem, as shown in Figur e 2- 2 . Figure 2-2. A problem The technique of accessing members through a generic object of type Object is called late binding. Late means that whether the desired member is really there is not known until the statement is actually executed. In contrast, leaving Option Strict on and accessing members only through variables that have been declared as the appropriate type is known as early binding. Early means that whether the member access is legitimate is known at compile time. Late binding is less efficient than early binding because additional checks are needed at runtime to determine whether the requested member actually exists on the runtime object and, if it does, to access that member. The worst part of late binding is that it can mask certain program errors such as mistyped member names until runtime. In general, this is bad programming practice.

2.15 Interfaces

It is useful to make a distinction between a classs interface and its implementation. Conceptually, the interface of a class is the set of members that are visible to users of the class—i.e., the classs public members. The public members are thought of as comprising the classs interface because they are the only way that code outside of the class can interact i.e., interface with objects of that class. In contrast, the implementation is comprised of the classs code plus the set of members that are not public. It is possible to take this interface concept further and separate interface definition from class definition altogether. This has benefits that will be shown shortly. To define an interface, use the Interface statement: Public Interface ISomeInterface Sub SomeSub Function SomeFunction As Integer Property SomeProperty As String Event SomeEvent _ ByVal sender As Object, _ ByVal e As SomeEventArgs _ End Interface An interface declaration defines methods, properties, and events that will ultimately be implemented by some class or structure definition. Because interfaces never include any implementation, the declarations are headers only—never any implementation code; End Sub , End Function , or End Property statements; or property get or set blocks. There are no access modifiers Public , Private , etc. because all members of an interface are public by definition. By convention, interface names start with the letter I.