Early Binding v.s. Late Binding

G

Guest

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?

Thanks,
Chris A.
 
A

Armin Zingler

Chris Adams said:
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?

Early binding means that the access to the member of an object can be
resolved at compile time. Late binding means that the existence of the
member is checked at run time. Early binding is much faster because the
member doesn't have to be searched at run time. In addition, late binding
might cause an exception if the member won't be found. This can not happen
using early binding because the compiler can check the existence of a
member, consequently *you* find the fault, not your customer will report an
exception.
 
C

Cor

Hi Chris,

In addition from Armin, you can prevent the exception in run time when you
set Option Strict On.
Late Binding is than not allowed, but than there is also not any performance
difference anymore between C# and VB.net.

:)

Cor
 
H

Herfried K. Wagner [MVP]

* "Cor said:
In addition from Armin, you can prevent the exception in run time when you
set Option Strict On.
Late Binding is than not allowed, but than there is also not any performance
difference anymore between C# and VB.net.

And always keep 'Option Brain' turned on.

;-)
 
P

Patrick Steele [MVP]

In addition from Armin, you can prevent the exception in run time when you
set Option Strict On.
Late Binding is than not allowed

By "not allowed" it means that the VB.NET compiler will not generate
code to support late binding. Late binding can still be done by the
programmer using Reflection.
 
C

Cor

Hi Patrick

First time I saw Armin not mention Option Strict On when there was a change
to do it.

I would not have written this if that had not been, the message from Armin
was clear enough, while the poster probably already did know that.

Herfried knew that of course.

:))))

Cor
 
H

Herfried K. Wagner [MVP]

* Patrick Steele said:
By "not allowed" it means that the VB.NET compiler will not generate
code to support late binding. Late binding can still be done by the
programmer using Reflection.

ACK -- but that's not as easy to use as VB.NET's "built in" late
binding.
 
A

Armin Zingler

Cor said:
First time I saw Armin not mention Option Strict On when there was a
change to do it.

I would not have written this if that had not been, the message from
Armin was clear enough, while the poster probably already did know
that.

As Patrick mentioned he was reading a book about it, I thought I don't have
to mention Option Strict.

:)
 
H

Herfried K. Wagner [MVP]

* "Armin Zingler said:
Using Reflection is also called late binding?

AFAIK that's late binding too because "bindings" are determined at
runtime.
 
A

Armin Zingler

Herfried K. Wagner said:
AFAIK that's late binding too because "bindings" are determined at
runtime.

What is later: Implicit late binding by the compiler, or explicit late
binding using reflection? ;-)))
 
J

Jay B. Harlow [MVP - Outlook]

Armin,
Using Reflection is also called late binding?
In addition to Herfried's comments. It may be more accurate to say that
Reflection also includes Late Binding.

As the two "major" features of Late Binding IMHO are to find the method &
invoke the method. Reflection supports both of these.

However Reflection also supports other features that are not used during
Late Binding...

Just a thought
Jay
 
J

Jay B. Harlow [MVP - Outlook]

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top