VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

VB .NET - Learn Visual Basic .NET

Systems Analysis - System analysis and Design tutorial for Software Engineering

You are here: Visual Basic > Advanced VB6 tutorial > Chapter 10

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


Dim objMyClass As MyComp.MyClass


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


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

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.

NOTE - Additional Support for Early Binding in the IDE: When you write code that 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.

When to Use Late Binding: You don't need to set a reference to a component 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.

IMPORTANT - Only Recent Versions of Excel Support Early Binding: Early binding onlyworks with versions 7.0 and later of Excel. If your Excel component is a version earlier than 7.0, you must use late binding.

Can't Use As New to Declare an Object With Late Binding: If you 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.


<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved