Link to home
Start Free TrialLog in
Avatar of dipeshpt
dipeshpt

asked on

Late Binding

give me brief intro on late binding
Avatar of bobbit31
bobbit31
Flag of United States of America image

late binding is when you bind an object to a variable at runtime rather than at compile time.

ie:

Dim lateBound as Object
set lateBound = CreateObject("Word.Application")

as opposed to:

Dim earlyBound as New Word.Application
btw: if you look it up in msdn, there is a pretty good explanation
ASKER CERTIFIED SOLUTION
Avatar of Brian Mulder
Brian Mulder
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Because with late binding every object reference requires extra resolution at runtime (to determine what kind of object it is etc.) it is considerably slower than early binding, where the object reference is compiled with the exe.  Early binding is preferred in all situations unless:

* For some reason you don't want to add a reference to your project (if for example, you are using excel and you want it to work with excel 97 and excel 2000, you would use the late binding technique and you can avoid adding a reference to the excel library in your project.)

* You just don't know what kind of object you will be referencing with the variable.  It is possible to write generic code that handles a wider variety of objects (as long as they share the same methods, properties etc.)

* You are in an environment such as VBScript that doesn't allow variable typing.  You do not have early binding as an option then.
Early binding also gives you the use of the VB IDE's auto-complete feature, which is very handy for developing.  However, because of Paul's first exception above, I often go back and switch all of my Early Binding statements to Late Binding statements after the original development is done.  

One other thing I suspect from my experience, is that you will get more complete resource deallocation using late binding than you will from early binding.  If you do this:

Dim lateBound as Object
set lateBound = CreateObject("Word.Application")

Set lateBound = Nothing

I suspect that you get better resource deallocation than:

Dim earlyBound as New Word.Application
Set earlyBound = Nothing

But I have nothing substantial to base that on.

Avatar of priya_pbk
priya_pbk

hi dipeshpt,

There are advantages and disadvantages on using Early or Late Binding. Here let me explain:

-Late Binding:
LateBinding(LB) uses "CreateObject" to create an instance of the application object(like Word, excel etc) which you may want to use in your appliction.
eg: To create an instance of Excel, by using late binding..
Dim objExcel as Object
Set objExce=CreateObject("Excel.Application")

Advantages of late Binding:
1) The main and the MOST important advantage of late binding is that your application is more certain to be 'version dependent'

eg: if you set your reference(ie by early binding) in your application to "Microsoft Word 10.0 Object Library", then the appln will run where the machine has Windows 2000 but cannot run on any lower versions (happened to my application deleivered to my client)
Therefor when you late bind, it will create the object with respect to the available version in that Pc.

2)With numerous references, the file size increases and takes longer to complile.

-------------------------------------------------------

-Early Binding:
a)First of all you need to set a reference in your project to the application you want to manipulate. This you can do so by going to your Menu in your appln, Project-References and choosing the object you would like to use(ie word or excel or whatever)


b)Then to create a new instance of that referenced application by using Early Binding..
Dim objEcxel as Excel.Application
Set objExcel=New Excel.Application

Advantages of Early Binding:
1)Code runs faster, cos it is compliled upfront with the appliction object(ie the appln knows the object which it is going to use)

2)Debugging is easier
3)Also one can have access ot intellisense ie one can get all the help by pressing F1 plus all the functions, methods and properties are available at design time itself.



I hope this gave you some insight on how both works.

-priya


sorry it should be "Version Independent" in advantages of Late Binding-(typo error")
The only thing that determines early or late binding is how you dim your variable

early binding : dim x as Word.Application

Set x = CreateObject("Word.Application")
or
Set x = New Word.Application


late binding : dim x as Object

you can use
Set x = CreateObject("Word.Application")

If you create your object using the New operator, Visual Basic uses the so-called internal instancing: The object is created internally, without passing through COM. The Instancing property is ignored—otherwise, it wouldn't be possible to instantiate Private objects.


If you create your object using the CreateObject function, the request goes through COM and is subject to the rules enforced by the class's Instancing property, which means that the operation will be successful only if the class is Public and creatable.

 

listening..
Avatar of DanRollins
Hi dipeshpt,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept bruintje's comment(s) as an answer.

dipeshpt, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange