Permalink
Fetching contributors…
Cannot retrieve contributors at this time
66 lines (56 sloc) 3.42 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.topic dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author translation.priority.ht
Early and Late Binding (Visual Basic)
2015-07-20
.net
devlang-visual-basic
article
VB
early binding
objects [Visual Basic], late-bound
objects [Visual Basic], early-bound
objects [Visual Basic], late bound
early binding, Visual Basic compiler
binding, late and early
objects [Visual Basic], early bound
Visual Basic compiler, early and late binding
late binding
late binding, Visual Basic compiler
d6ff7f1e-b94f-4205-ab8d-5cfa91758724
10
dotnet-bot
dotnetcontent
cs-cz
de-de
es-es
fr-fr
it-it
ja-jp
ko-kr
pl-pl
pt-br
ru-ru
tr-tr
zh-cn
zh-tw

Early and Late Binding (Visual Basic)

The [!INCLUDEvbprvb] compiler performs a process called binding when an object is assigned to an object variable. An object is early bound when it is assigned to a variable declared to be of a specific object type. Early bound objects allow the compiler to allocate memory and perform other optimizations before an application executes. For example, the following code fragment declares a variable to be of type xref:System.IO.FileStream:

[!code-vbVbVbalrOOP#90]

Because xref:System.IO.FileStream is a specific object type, the instance assigned to FS is early bound.

By contrast, an object is late bound when it is assigned to a variable declared to be of type Object. Objects of this type can hold references to any object, but lack many of the advantages of early-bound objects. For example, the following code fragment declares an object variable to hold an object returned by the CreateObject function:

[!code-vbVbVbalrOOP#91]

Advantages of Early Binding

You should use early-bound objects whenever possible, because they allow the compiler to make important optimizations that yield more efficient applications. Early-bound objects are significantly faster than late-bound objects and make your code easier to read and maintain by stating exactly what kind of objects are being used. Another advantage to early binding is that it enables useful features such as automatic code completion and Dynamic Help because the [!INCLUDEvsprvs] integrated development environment (IDE) can determine exactly what type of object you are working with as you edit the code. Early binding reduces the number and severity of run-time errors because it allows the compiler to report errors when a program is compiled.

[!NOTE] Late binding can only be used to access type members that are declared as Public. Accessing members declared as Friend or Protected Friend results in a run-time error.

See Also

xref:Microsoft.VisualBasic.Interaction.CreateObject%2A
Object Lifetime: How Objects Are Created and Destroyed
Object Data Type