Early binding and later binding in. Net (2), early binding of. net

Source: Internet
Author: User

Early binding and later binding in. Net (2), early binding of. net

The previous article introduced the background of binding in. Net and introduced "Early binding and later binding" at the end of the article. This article will give a brief introduction to binding in. Net.

Early binding early binding: If. Net can know the object to which the property or method belongs during compilation, the DispID or vtable address of the member in the Type Library can be searched 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 Class, the variable can only store the reference of the Class Object .. Net can use early binding for all attributes and methods called for this variable.
Later bound. net has a base class Object. All classes are inherited from the base class. When we declare a variable and do not specify the type of the variable ,. net will declare this variable As Object ,. net cannot determine which type of object the variable will reference during compilation. Therefore,. Net must use post-binding to determine whether the object's attributes and methods can use the variable at runtime.
After learning about early binding and later binding, let's look at our code:

'Post-binding, whose Object type is Object Dim iu = createuser. produceuser', early binding, whose Object type is IUser Dim iu as IDAO. IUser = createuser. ProduceUser
Object Type of variables in the program:


We have been using early binding. Do you still remember Variant (variable type) in VB? It is a kind of late binding that we first started to contact. Now we use the generic type when we are working on the data center, later binding is also involved.

Differences between early binding and later binding

Early binding: Early binding objects are much faster than late binding objects, and the code is easier to read and maintain by specifying the object types used. Another advantage of early binding is that it enables useful functions such as automatic Code Completion and dynamic help, because Visual Studio integrates the development environment (IDE) you can accurately determine the object type when editing the code. Since early binding allows the compiler to report errors during program compilation, it reduces the number and severity of errors during runtime.

Post-binding: later binding. Each time you call a property or method, Visual Basic sends 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.

Summary: there is great wisdom in small issues, and we have learned that early binding and later binding should be used with caution in our future programming. Have you noticed that in the previous article, we can use post-binding in the method body to obtain variable member variables, because. Net has a type inference mechanism. With regard to type-based reasoning, you can check the knowledge about this aspect on the Internet. I will not introduce it here... PS: it will not be the motive force behind the discovery of the problem. Continue to maintain...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.