INFO: Late, ID, Early Binding Types Possible in VB for Apps

This article was previously published under Q138138
This article has been archived. It is offered "as is" and will no longer be updated.
There are three kinds of binding that Visual Basic for Applicationssupports:

  1. Late Binding.
  2. ID Binding.
  3. Early\Vtable Binding.
OLE Automation controllers can use the OLE IDispatch interface to gainaccess to objects that implement this interface. Although programmers knowobject members (methods and properties) by name, IDispatch keeps track ofthem internally by a number -- the Dispatch ID (DISPID). Before an OLEAutomation controller can find a method or property, it must have theDISPID that maps to the name of the member.

Once it has the DISPID, it can then call IDispatch::Invoke to locate theproperty or invoke the method, packaging the parameters for the property ormethod into one of the IDispatch::Invoke parameters. The object'simplementation of IDispatch::Invoke must then unpack the parameters,call the property or method, and handle any errors that might occur. Whenthe property or method returns, the object passes its return value back tothe controller through an IDispatch::Invoke parameter.

Late Binding

DISPIDs are available at run time and, in some circumstances, at compiletime. At run time, controllers get DISPIDs by calling theIDispatch::GetIDsOfNames function. This is called late binding (or latename binding) because the controller binds to the property or method at runtime.

Late Binding is the slowest and easiest to support, and it is the onlymechanism that Visual Basic version 3.0 supports. The target object mustsupport the IDispatch OLE interface for this to work. A typelib (arepository of object property, object method, and object method parameterinformation) is not required for the object as IDispatch::GetIDsOfNames isfirst called at run time to bind the name to its DISPID and thenIDispatch::Invoke is called again at run time on the target object toactually execute the property or method. As a result, there is no compile-time type checking. You get errors like "name not found" only at run time.

Here's an example of Visual Basic code that implements late binding:
   Dim obj As Object   Set obj = New Class1   Call obj.somemethod				
Here the type of "Object" is not known until the Set statement is executedat run time.

ID Binding

The DISPID of each property or method is fixed and is part of the object'stype description. If the object is described in a type library, an OLEAutomation controller can read the DISPIDs from the type library at compiletime, and thus avoid calling IDispatch::GetIDsOfNames. This is called IDbinding. Because it requires only one call to IDispatch (that is, the callto invoke) rather than the two calls required by late binding, it isgenerally about twice as fast.

ID binding is actually a form of early binding, as GetIDsOfNames islogically called at compile time, so a typelib is required. The targetobject must support the OLE IDispatch interface, so that Invoke can becalled at run time on the target object. The advantages of this are thatperformance is better because no binding is needed at run time, youget better compile-time type checking, and you receive more informativeerror messages.

Early Binding

In early binding (also known as VTable binding), the target object need notnecessarily support IDispatch; it can support any custom interface. If theobject does support IDispatch, early binding works because the object nowsupports a dual interface (a combination of an IDispatch and a VTableinterface). Early binding does not use DISPIDs and will not work on astraight IDispatch interface implementation. A typelib for the objectis required in order get the DISPIDs. A compile-time binding generates codeto call any of the object's methods or properties through that object'sVTable.

The advantages of early binding are that you get compile-time type checkingbenefits and the run-time performance is better, especially when anin-process OLE Server is involved. This is because there is no need toindirect the call to a member through an implementation ofIDispatch::Invoke. Basically, with VTable binding on an in-process server,function calls are direct function calls. If the OLE server is an .exefile, then it will be slower due to the remoting code that has to beaccounted for.

Here is an example of Visual Basic code that implements early binding:
   Dim obj As Class1   Set obj = New Class1   Call obj.somemethod				
Note that it is up to the server object to determine whether it supportsearly binding or not. All objects created in Visual Basic version 4.0support all the three styles of binding. Visual Basic for Applicationswill use early binding in preference to ID binding, so an object thatsupports both will be early bound. If, in the previous example, Class1 wereimplemented by an OLE server that supported only ID binding, then IDbinding would be used. There is no way to tell from Visual Basic codewhether you are using early binding or ID binding.

Article ID: 138138 - Last Review: 02/24/2014 08:22:45 - Revision: 2.0

  • Microsoft Visual Basic 4.0 Standard Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 16-bit Enterprise Edition
  • Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
  • kbnosurvey kbarchive kbinfo kbprogramming KB138138