Sie sind auf Seite 1von 3

Early Binding and Late Binding http://visualbasic.about.com/od/usingvbnet/a/earlybind.

htm

Visual Basic

Early Binding and Late Binding Free Visual Basic Newsletter!


Email Print
What they are and why you should care Enter email address
By Dan Mabbutt, About.com
Discuss in my Forum

See More About: early binding and late binding in visual basic .net data types in visual basic .net why early
binding is better

You see the phrases, "early binding" and "late binding" quite frequently in Microsoft documentation and web pages (like
this one) but they often simply assume that you understand why these concepts are important. This article fills in the gaps!
(Note ... this article was written for, and tested with VB.NET 2005 Express Edition but the concepts apply to any version.)

The "textbook" definition goes something like this (copied from Microsoft's MSDN page):

"An object is early bound when it is assigned to a variable declared to be of a specific object type."

What the heck does that mean?

To really understand this concept, you have to go back to bedrock principals and remember that the computer doesn't
understand anything about data types. (See Data types in VB.NET.) It's all 1's and 0's to the computer. Only the software
understands that a memory location is a "Char" or a "Decimal" or something else. And a variable in the software,
something like ...

Dim MyVariable as <variable data type>

... doesn't always tell the software. If the variable is declared as ...

Dim MyVariable as Object

... then the variable is said to be "late bound" because the software doesn't really know what it is until runtime when it's
actually used for something.

Here's an example of late binding:

Dim MyVariable As Object


MyVariable = "About Visual Basic"
Debug.WriteLine("The variable data type is: " & MyVariable.GetType.ToString)
MyVariable = #6/30/2006#
Debug.WriteLine("The variable data type is: " & MyVariable.GetType.ToString)

In this case, MyVariable is simply converted to whatever data type is necessary. When the code is run, the result in the
Immediate window of VBE is:

The variable data type is: System.String


The variable data type is: System.DateTime

1 of 3 1/4/2009 5:27 PM
Early Binding and Late Binding http://visualbasic.about.com/od/usingvbnet/a/earlybind.htm

This is, by the way, very close to what happens in VB 6 when a variant data type is used. It's also what happens in
VB.NET when Option Explicit is set to Off and a variable isn't declared at all.

Whenever possible, you should use "early binding" and declare variables as a specific type. There are basically three
reasons:

1. It makes your program run faster since code to convert to the actual type isn't necessary.
2. The compiler can help find errors that might otherwise cause a runtime program crash.
3. Intellisense code completion and Dynamic Help only works with early bound variables.

To illustrate the first reason, here's a quick program using the new Framework 2.0 Stopwatch object to time a loop with
both early and late binding:

Dim ExecutionTimer As New Stopwatch


Dim i As Integer
Dim MyVariable As Object
'Dim MyVariableStr As String
'Dim MyVariableDte As Date

ExecutionTimer.Start()
For i = 1 To 100000
'MyVariableStr = "About Visual Basic" & Chr(i Mod 64)
'MyVariableDte = #6/30/2006#
'MyVariableDte.AddSeconds(1)
MyVariable = "About Visual Basic" & Chr(i Mod 64)
MyVariable = #6/30/2006#
MyVariable.AddSeconds(1)
Next
ExecutionTimer.Stop()

Debug.WriteLine( _
"Total Milliseconds Elapsed: " & _
ExecutionTimer.Elapsed.TotalMilliseconds.ToString)

(MyVariable is incremented instead of just assigning a constant value as in the first example because the Visual Basic
compiler will optomize the execution if it "knows" that a constant value is always used. Smart compiler!)

To execute the same code with early binding, just reverse the commented code and run it again. The result on my
machine is:

Total Milliseconds Elapsed: 4405.2836 (late binding)


Total Milliseconds Elapsed: 52.6874 (early binding)

Early binding made the code run almost 100 times as fast.

You will see the second reason illustrated if you edit the example program above. The illustration shows how much more
"help" you get from Intellisense when a data type is declared.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

To illustrate the third advantage, my article The Date Bug in VB 6 to VB.NET Conversion shows a runtime error that could

2 of 3 1/4/2009 5:27 PM
Early Binding and Late Binding http://visualbasic.about.com/od/usingvbnet/a/earlybind.htm

have been prevented with explicit declaration of data types.

Explore Visual Basic


See More About: Must Reads
early binding and late why early binding is About Visual Basic and This
binding in visual basic better Site
.net Learn Visual Basic .NET
data types in visual Learn Visual Basic With VB 6
basic .net A Glossary of Technical Terms

By Category Most Popular


Learn VB 6 Use VB 6 Part 1 of an Intro to VB.NET
Using VB.NET VB Books Learn Visual Basic Version 6
Learn VB.NET Applications Learning Visual Basic
Programming The Tic Tac Toe
Game
Part 2 of an Intro to VB.NET

3 of 3 1/4/2009 5:27 PM

Das könnte Ihnen auch gefallen