Click to See Complete Forum and Search --> : vtable binding vs early binding vs late binding
bhaskerg
Mar 7th, 2001, 06:23 AM
can someone explain the differences between v table binding, early binding and late binding with an example...
thanx in advance
hayessj
Mar 7th, 2001, 07:26 AM
straight out of MSDN ...
Object references are early-bound if they use object variables declared as variables of a specific class. Object references are late-bound if they use object variables declared as variables of the generic Object class. Object references that use early-bound variables usually run faster than those that use late-bound variables.
For example, you could assign a reference to an Excel object to either of the following variables:
Dim xlApp1 As Excel.Application
Set xlApp1 = New Excel.Application
Dim xlApp2 As Object
Set xlApp2 = CreateObject("Excel.Application")
Code that uses variable xlApp1 is early-bound and will execute faster than code that uses variable xlApp2, which is late-bound.
Late Binding
When you declare a variable As Object, Visual Basic cannot determine at compile time what sort of object reference the variable will contain. In this situation, Visual Basic must use late binding— that is, Visual Basic must determine at run time whether or not that object will actually have the properties and methods you used in your code.
For example, Visual Basic will compile the following code without generating errors, even though it refers to a method that doesn't exist, because it uses a late-bound object variable. It doesn't check for the existence of the method until run time, so it will produce a run-time error:
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.TheImpossibleMethod ' Method doesn't exist.
This code runs slower than code that uses an early-bound object variable because Visual Basic must include code in the compiled executable that will determine at run time whether or not the Microsoft Excel Application object has a TheImpossibleMethod method.
Although late binding is the slowest way to invoke the properties and methods of an object, there are times when it is necessary. For example, you may write a function that uses an object variable to act on any of several different classes of objects. Because you don't know in advance what class of object will be assigned to the variable, declare it as a late-bound variable using As Object.
Early Binding
If Visual Basic can detect at compile time what object a property or method belongs to, it can resolve the reference to the object at compile time. The compiled executable contains only the code to invoke the object's properties, methods, and events. This is called early binding.
When you declare an object variable using the class that defines the object, the variable can only contain a reference to an object of that class. Visual Basic can use early binding for any code that uses the variable.
Early binding dramatically reduces the time required to set or retrieve a property value, because the call overhead can be a significant part of the total time. For method calls, the improvement depends on the amount of work the method does. Short methods, where the call overhead is comparable to the time required to complete the task, will benefit the most.
bhaskerg
Mar 8th, 2001, 12:42 AM
but what is this v table binding?
hayessj
Mar 8th, 2001, 03:03 AM
Straight out of MSDN (again heh-heh), there are some very good articles on msdn.microsoft.com you should check out for learning about binding etc.
Binding is the process of setting up a property or method call that’s to be made using a particular object variable. It’s part of the overhead of calling the property or method.
The time required to call a procedure depends on two factors:
The time required to perform the task the procedure was designed to do, such as finding the determinant of a matrix.
The overhead time required to place the arguments on the stack, invoke the procedure, and return.
As a component author, you’ll do everything you can to minimize the first item. The second item, however, is not entirely under your control.
The overhead for a method call depends on the type of binding Visual Basic uses for the method call, which in turn depends on the way a client application declares object variables, which in turn depends on the developer of the client application.
To ensure that developers who use your component get the best possible performance, you may want to include the information in this topic in the Help file for your component.
Note Binding affects all property and method calls, including those the objects in your component make to each other. Thus the binding issues discussed here can also affect the internal performance of your component.
Types of Binding
There are two main types of binding in Automation — late binding and early binding. Early binding is further divided into two types, referred to as DispID binding and vtable binding. Late binding is the slowest, and vtable binding is the fastest.
Late Binding
When you declare a variable As Object or As Variant, Visual Basic cannot determine at compile time what sort of object reference the variable will contain. Therefore, Visual Basic must use late binding to determine at run time whether the actual object has the properties and methods you call using the variable.
Note Late binding is also used for variables declared As Form or As Control.
Each time you invoke a property or method with late binding, Visual Basic passes the member name to the GetIDsOfNames method of the object’s IDispatch interface. GetIDsOfNames returns the dispatch ID, or DispID, of the member. Visual Basic invokes the member by passing the DispID to the Invoke method of the IDispatch interface.
For an out-of-process component, this means an extra cross-process method call, essentially doubling the call overhead.
Note You cannot call the methods of the IDispatch interface yourself, because this interface is marked hidden and restricted in the Visual Basic type library.
Early Binding
If Visual Basic can tell at compile time what object a property or method belongs to, it can look up the DispID or vtable address of the member in the type library. There’s no need to call GetIDsOfNames.
When you declare a variable of a specific class — for example, As Widget — the variable can only contain a reference to an object of that class. Visual Basic can use early binding for any property or method calls made using that variable.
This is the recommended way to declare object variables in Visual Basic components and applications.
Important Whether early or late binding is used depends entirely on the way variables are declared. It has nothing to do with the way objects are created.
Tip Early binding dramatically reduces the time required to set or retrieve a property value, because call overhead is a significant fraction of the total call time.
vTable Binding
In the fastest form of early binding, vtable binding, Visual Basic uses an offset into a virtual function table, or vtable. Visual Basic use vtable binding whenever possible.
Objects created from Visual Basic class modules support all three forms of binding, because they have dual interfaces — that is, vtable interfaces derived from IDispatch.
If client applications declare variables using explicit class names, Visual Basic objects will always be vtable bound. Using vtable binding to call a method of an in-process component created with Visual Basic requires no more overhead than calling a function in a DLL.
Note For in-process components, vtable binding reduces call overhead to a tiny fraction of that required for DispID binding. For out-of-process components the change is not as great — vtable binding is faster by a small but significant fraction — because the bulk of the overhead comes from marshaling method arguments.
DispID Binding
For components that have type libraries but don’t support vtable binding, Visual Basic uses DispID binding. At compile time, Visual Basic looks up the DispIDs of properties and methods, so at run time there’s no need to call GetIDsOfNames before calling Invoke.
Note While you can ensure that early binding is used (by declaring variables of specific class types), it’s the component that determines whether DispID or vtable binding is used. Components you author with Visual Basic will always support vtable binding.
bhaskerg
Mar 8th, 2001, 03:31 AM
can anyone give an simple and clear example for all the 3 types of bindings?
bhaskerg
Mar 8th, 2001, 03:42 AM
I understand that binding depends on the way u declare the objects....if that is the case what is the difference between these two?
1)
Dim xlApp2 As Object
Set xlApp1 = New Excel.Application
2)
Dim xlApp2 As Object
Set xlApp2 = CreateObject("Excel.Application")
Do both use late binding?
hayessj
Mar 8th, 2001, 04:26 AM
1)
Dim xlApp2 As Object
Set xlApp1 = New Excel.Application
2)
Dim xlApp2 As Object
Set xlApp2 = CreateObject("Excel.Application")
Both of these use late binding because they are declared as Object. There is no difference between New and CreateObject when creating external objects (such as Excel objects) so both of these are examples of late binding.
Examples of early binding
1)
Dim xlApp2 As Excel.Application
Set xlApp1 = New Excel.Application
2)
Dim xlApp2 As Excel.Application
Set xlApp2 = CreateObject("Excel.Application")
3)
Dim xlApp3 As New Excel.Application
These are all early bound because they are decalred as a specific type.
For all intents and purposes there are only two types of binding, late and early. DispID and vTable binding are both types of early binding but you have no control over which is used, that depends on the object you are creating so you can view them as the same, i.e. early binding.
bhaskerg
Mar 8th, 2001, 04:41 AM
Thanks a lot hayessj....u have cleared my doubts but again can u tell me what is the difference between "new" and "createobject" ? and also what happens.....during
Dim xlApp2 As Excel.Application
and what happens...
Set xlApp1 = New Excel.Application
hayessj
Mar 8th, 2001, 05:19 AM
When you are creating an external object, such as an Excel object in your example there is no difference between using new and CreateObject because they both use COM object creation services. So both of these work the same way.
1)
Dim xlApp2 As Excel.Application
Set xlApp1 = New Excel.Application
2)
Dim xlApp2 As Excel.Application
Set xlApp2 = CreateObject("Excel.Application")
There is only a difference between New and CreateObject whe you are creating classes in your project. In that case Create Object will use the standard COM object creation services whereas New will use a private faster implementation of COM object creation services.
So to summarize, there is no difference between New and CreateObject except for creating instances of classes in your project where New is faster.
If you are interested in getting into the guts of COM and how it works in detail I highly recommend this book, definitely one of the best VB books I've read ...
"Programming Distributed Applications with COM+ and Microsoft Visual Basic 6.0" by Ted Pattison
ISBN: 073561010X
The link to it on Amazon is:
http://www.amazon.com/exec/obidos/ASIN/073561010X/o/qid=984049972/sr=2-2/105-5317652-6175909
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.