Late and Early Binding of Object Variables

You can implement class objects with object variables by declaring them as either generic object-type variables or variables of the specific type provided in the server's class library. For example, Dim objMyClass As New MyComp.MyClass or

Dim objMyClass As MyComp.MyClass and

Dim objMyClass As Object would all be valid declarations for an object that will later point to an instance of the MyComp component server's class named MyClass.

The first two declarations are preferable, however, as they provide early binding of the MyClass class in your application while the third, more generic declaration, is an example of late binding.

Binding refers to the point at which a system recognizes references to external objects in the compile-run cycle. For instance, you might misspell a declared object's property name in your code. Knowing whether the object is early- or late-bound will tell you when the system will detect the error.

If you've declared the object with early binding, the VB compiler can check the component's class library and catch any syntax errors before the application fully compiles and runs.

If, however, you use the As Object declaration to provide late binding, the VB compiler won't be able to check the component's class library and the compiler won't detect any syntax errors in the use of the object. Instead syntax errors with objects from the COM component will cause runtime errors in your application.

For instance, the Excel component server's most important object is the Application object. Application has a Visible property which, as you might expect, sets the object's visibility to a user. Suppose you have the following declaration in your code. Dim objExcel As Excel.Application or

Dim objExcel As New Excel.Application

And, later on, you have the following line. objExcel.Visable = True

The compiler catches the misspelling of the Visible property's name as soon as you try to run the application during design mode or when you try to make an executable file.

If, however, you'd declared the object with the line Dim objExcel As Object the compiler wouldn't be able to check the syntax of objExcel.Visable = True w Additional Support for Early Binding q in the IDE When you write code that 2 uses early-bound object variables, you'll notice that the VB runtime environment is able to recognize the object model behind the variable by offering you a drop-down list of possible members whenever you type the variable's name followed by a period.

w When to Use Late Binding You don't q need to set a reference to a compo-Z nent with the Project References dialog box if you're going to use late binding. For some components (those without an available Object Library, such as the versions of Microsoft Word through version 7.0), late binding is the only option because the application provides no Object Library to set a reference.

against Excel's class library. The error (visible is misspelled) goes undetected until such time as VB attempts to execute this line, at which point the application would generate a runtime error.

You must use late binding if a component server's class library isn't available to you. For instance, Microsoft Word for Windows 7.0 and below doesn't provide a class library for its object classes. You must, therefore, always use the As Object syntax to declare a Word 7.0 object.

EG Can't Use As New to Declare an KN Object With Late Binding If you

EN use late binding with an As Object declaration, you can't use the New keyword when you instantiate the object variable. You must use CreateObject or GetObject, as discussed in the following section.

Was this article helpful?

0 0

Post a comment