……………………………………………….Expertise in .NET Technologies

Early and Late Binding in VB.NET

Posted by Ravi Varma Thumati on March 24, 2009

The Visual Basic 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 FileStream:

' Add Imports statements to the top of your file.
Imports System.IO
'  Create a variable to hold a new object.
   Dim FS As FileStream
  ' Assign a new object to the variable.
   FS = New FileStream("C:\tmp.txt", FileMode.Open)

Because 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:

' To use this example, you must have Microsoft Excel installed on your computer.
Option Strict Off ' Option Strict Off allows late binding.
Sub TestLateBinding()
   Dim xlApp As Object
   Dim xlBook As Object
   Dim xlSheet As Object
   xlApp = CreateObject("Excel.Application")
   'Late bind an instance of an Excel workbook.
   xlBook = xlApp.Workbooks.Add
   'Late bind an instance of an Excel worksheet.
   xlSheet = xlBook.Worksheets(1)
   xlSheet.Application.Visible = True ' Show the application.
   ' Place some text in the second row of the sheet.
   xlSheet.Cells(2, 2) = "This is column B row 2"
End Sub

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 Visual Studio .NET 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 runtime error.



One Response to “Early and Late Binding in VB.NET”

  1. What would all of us do minus the marvellous thoughts you write about on this website? Who has the fortitude to deal with vital topics for the health of common visitors like me? I actually and my guys are very lucky to have your web blog among the kinds we usually visit. We hope you know how a great deal we love your effort! Best wishes from us all.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: