Later binding and early binding of VB6
1. What is post-binding and pre-binding and post-binding?
The definition of msdn 6 is as follows:
Post-binding: If you declare a variable as object or as variant (including variables of as form or as control ), visual Basic cannot determine the type of object that the variable will reference during compilation. Therefore, Visual Basic must use post-binding to determine whether the object's attributes and methods can use the variable at runtime.
If later binding is used, each time you call a property or method, Visual Basic must pass the member name to the getidsofnames method of the idispatch interface of the object. Getidsofnames returns the dispatch ID or dispid of the member. Visual Basic then sends the dispid to the invoke method of the idispatch interface to call the member.
For external components, this means an additional cross-process method call, which doubles the call overhead.
Pre-binding: If visual basic can know the object to which the property or method belongs during compilation, it can look for the dispid or vtable address of the member in the Type Library in advance. In this way, you do not need to call getidsofnames at runtime.
When the class of the variable is explicitly declared, such as widget, the variable can only store the reference of the Class Object. Visual Basic can be used to pre-bind all attributes and methods called for this variable.
We recommend that you useProgramTo declare object variables.
Using pre-binding or post-binding depends entirely on the way variables are declared. The object creation method has no effect on this.
2. Several advantages and benefits of later binding
First, I personally think that VB6 does not support implementation inheritance. For some designs, you can define the object as a variant variable to get some convenience in design and coding, and get the benefits of the interface or abstract class. For example:
For example, define two classes:
Class1:
1 Option Explicit
2
3 Public Sub Update ()
4 Msgbox " Class1 updated"
5 End sub
Class2:
1 Option Explicit
2
3 Public Sub Update ()
4 Msgbox " Class2 updated"
5 End sub
You can use the following methods to implement some design patterns:
1 Dim Objtest As Variant
2
3 Dim Objc1 As New Class1
4 Set Objtest = Objc1
5 Objtest. Update
6
7 Dim Objc2 As New Class2
8 Set Objtest = Objc2
9 Objc2.test
10
In this way, the advantages of abstract interfaces can be obtained without an inheritance mechanism, thus separating interfaces and implementations.
Second, when calling an external COM automation component, such as office, if you call it in this way, for different versions of office, if the methods and attributes you call exist, it can be adapted to different versions of office, but if you determine the object type in the project reference through pre-binding, there is a version compatibility problem.
3. Efficiency and disadvantage
In fact, the main disadvantage is efficiency. In the book "Advanced Visual Basic 6", the efficiency difference is 1.5 times when the variant and long variables are used for loop, which is not as big as others think, in addition, for large objects, the difference is not very big. For the example at the front of this article, if you create an in-process object, the call efficiency difference between pre-binding and post-binding is about 2: 3, that is, it is 1.5 times. For external COM automation objects such as office, there may be a big difference and there is no dedicated test. However, for office automation, the speed is mainly determined by the start and initialization of the Office itself.
Another disadvantage is that attributes and method members in the IDE are not automatically listed.
Therefore, I personally think that when using VB6, in order to get a good design, sometimes we may wish to use some later binding features of VB6 to improve the design quality.
Later, we will introduce some experience in improving the design by using this feature in GIS development. Thank you!