| You are here: | About>Computing & Technology>Visual Basic> Using VB.NET> Early Binding and Late Binding |
![]() | Visual Basic |
Early Binding and Late BindingWhat they are and why you should careYou 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 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 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:
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 (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) 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. -------- To illustrate the third advantage, my article The Date Bug in VB 6 to VB.NET Conversion shows a runtime error that could have been prevented with explicit declaration of data types. |
Las Vegas on a BudgetFind a BargainHotel DealsCheap EatsFree AttractionsEntertainment for Less |
All Topics | Email Article | | | ![]() |
| Advertising Info | News & Events | Work at About | SiteMap | Reprints | Help | Our Story | Be a Guide |
| User Agreement | Ethics Policy | Patent Info. | Privacy Policy | ©2008 About, Inc., A part of The New York Times Company. All rights reserved. |


