Sunday, February 1, 2009

Early and Late Binding

There are numerous definitions for early and late binding. Some are technical, some are theoretical. I will try to define it from both viewpoints.

Variables and expressions can be assigned at compile time or at runtime. Compile time (declaration time) assignment is called:

  • early binding
  • static binding
  • static typing

An example for early binding:

  1: '  Create a variable to hold a new object.
  2: Dim FS As System.IO.FileStream
  3: ' Assign a new object to the variable.
  4: FS = New System.IO.FileStream("C:\tmp.txt", _
  5:     System.IO.FileMode.Open)

Runtime assignment is called dynamic binding or late binding, when the type of the variable or expression is determined by the current (application) conditions. An example for late binding:

  1: ' To use this example, you must have Microsoft Excel installed on your computer.
  2: ' Compile with Option Strict Off to allow late binding.
  3: Sub TestLateBinding()
  4:     Dim xlApp As Object
  5:     Dim xlBook As Object
  6:     Dim xlSheet As Object
  7:     xlApp = CreateObject("Excel.Application")
  8:     ' Late bind an instance of an Excel workbook.
  9:     xlBook = xlApp.Workbooks.Add
 10:     ' Late bind an instance of an Excel worksheet.
 11:     xlSheet = xlBook.Worksheets(1)
 12:     xlSheet.Activate()
 13:     ' Show the application.
 14:     xlSheet.Application.Visible = True
 15:     ' Place some text in the second row of the sheet.
 16:     xlSheet.Cells(2, 2) = "This is column B row 2"
 17: End Sub

Another example for late binding:

  1: Dim MyVariable As Object
  2: MyVariable = "About Visual Basic"
  3: Debug.WriteLine("The variable data type is: " & MyVariable.GetType.ToString)
  4: MyVariable = #6/30/2006#
  5: Debug.WriteLine("The variable data type is: " & MyVariable.GetType.ToString)

The result for the code above:

The variable data type is: System.String
The variable data type is: System.DateTime

As you can see, the variable named MyVariable can represent a System.String and a System.Date in the code above.

Despite of the advantages of late binding, early binding is recommended:

  • Early binding code can be optimized better by the compiler, so it runs (much) faster
  • It is usually easier to read early binding code (known types, etc.)
  • Automatic code completion, Dynamic Help and other advanced IDE features are available only for early binding codes
  • Compiler can type 'compatibility' for early binding variables and expressions
  • Application constants are accessible (e.g. in MS Word)

Some advantages of late binding:

  • Version independence. E.g. in case of MS Word, programming against Word 97 and changing code libraries to Word 2000 can be smooth and automatic.
  • More references can mean more linking and bigger file size (in case of unmanaged code)

There are numerous methods in C# to use late binding. Most of them employs reflection and dynamic compilation. There will be late binding as a language feature from C# 4.0 specification. Late binding will be as simple as the following:

  1: dynamic d = GetDynamicObject();
  2: d.Foo();
Late binding will be achieved by the dynamic keyword.

No comments:

Post a Comment