Solved

Late Binding

Posted on 2002-06-19
11
516 Views
Last Modified: 2010-05-02
give me brief intro on late binding
0
Comment
Question by:dipeshpt
11 Comments
 
LVL 18

Expert Comment

by:bobbit31
Comment Utility
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
0
 
LVL 18

Expert Comment

by:bobbit31
Comment Utility
btw: if you look it up in msdn, there is a pretty good explanation
0
 
LVL 44

Accepted Solution

by:
bruintje earned 50 total points
Comment Utility
Hi dipeshpt,

this explanation is also very clear
http://edocs.bea.com/wls/docs70/jcom/overview.html#1107478

HAGD:O)Bruintje
0
 
LVL 38

Expert Comment

by:PaulHews
Comment Utility
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.
0
 
LVL 18

Expert Comment

by:mdougan
Comment Utility
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.

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 2

Expert Comment

by:priya_pbk
Comment Utility
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


0
 
LVL 2

Expert Comment

by:priya_pbk
Comment Utility
sorry it should be "Version Independent" in advantages of Late Binding-(typo error")
0
 
LVL 2

Expert Comment

by:corvanderlinden
Comment Utility
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.

 

0
 

Expert Comment

by:nickwoolley
Comment Utility
listening..
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
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
0
 

Expert Comment

by:SpideyMod
Comment Utility
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now