Chris,
In addition to all the other comments.
Late Binding is when you declare your variables as Object and invoke methods
on those Objects. Early Binding is when you declare your variables as a
specific type then invoke methods on those (typed) objects.
For example the following uses Early Binding as we explicitly declare reader
as a IO.StreamReader, when VB.NET compiles the program it knows what methods
& properties that reader has available and calls them:
Dim reader As IO.StreamReader
Dim line As String
reader = New IO.StreamReader("myfile.txt")
line = reader.ReadLine()
Do Until line Is Nothing
Debug.WriteLine(line, "line")
line = reader.ReadLine()
Loop
reader.Close()
While the following uses Late Binding as we declared reader as Object, when
VB.NET compiles the program it DOES not know what methods & properties that
reader has (other then the ones on Object itself) so at run time it will
need to see if reader has a ReadLine method, if it does it calls it.
Dim reader As Object
Dim line As String
reader = New IO.StreamReader("myfile.txt")
line = reader.ReadLine()
Do Until line Is Nothing
Debug.WriteLine(line, "line")
line = reader.ReadLine()
Loop
reader.Close()
This runtime checking is a performance issue (however it can also increase
flexibility) so its a trade off. Most of the time you want Early Binding on
the rare occasion when I need Late Binding (COM interop usually) I try to
isolate that to a single file in my project... You want Early Binding as
normally identifies problems at compile time and it has better performance.
As the others have mentioned you control the availability of Late Binding by
using Option Strict Off, I normally include Option Strict On at the top of
each file, so I do not inadvertently use Late Binding...
By using "published" Interfaces you can achieve the effect of Late Binding
while still enjoying the benefits of Early Binding, however that is a
discussion for a different thread. ;-)
Hope this helps
Jay
Chris Adams said:
Hello,
I've been upgrading some sample projects from VB6 to .NET. In the book I
am reading on how to do this, there are many references to Early Binding &
Late Binding, but the book doesn't give a clear explanation of what they
mean. Could somebody please explain?