Hello,
Can any one tell me what is early binding and late binding in context of objects.
what is difference between
dim obj as new data
and
set obj=new data
where and when we use these.
Printable View
Hello,
Can any one tell me what is early binding and late binding in context of objects.
what is difference between
dim obj as new data
and
set obj=new data
where and when we use these.
Early binding - You have a reference to a library included in your project, and you use the objects in the library like this...
VB Code:
Dim obj As [b]MyObject[/b] Set obj = [b]New MyObject[/b]
Late binding - You may or may not have a reference in your project, and you use objects like this...
VB Code:
Dim obj As [b]Object[/b] Set obj = [b]CreateObject("MyLib.MyObject")[/b]
The second relies on the latest version of the interface to the object as dictated by the registry, rather than the reference in the project.
As for your second question, when you instantiate an object inline, the Class_Initialize sub isn't really called until the object is first referenced. When you say Set obj = New MyObject, the Class_Initialize sub is called right then.
Early Binding:
VB Code:
Public Sub EarlyBoundArgument(objWorkbook as Excel.WorkBook) objWorkbook.ActiveSheet.Select End Sub
Late Binding:
VB Code:
Public Sub LateBoundArgument(objWorkbook as Object) objWorkbook.ActiveSheet.Select End Sub
The major diference is the VB IDE can catch errors before you compile in the case of Early Binding.
With Late Binding, the code might not even work and the compiler will still give you a valid executable that may crash at runtime.
SO basically Late binging happens at run-time and early binding happens at compile time.
crptcblade is correct about the registry. Late binding enables you to create a program that eg works for Microsoft Word. You would then define an object and create the object 'later' based on a Word document.
With early binding you would set your Word 'dlls' into the references of the project and bind that way.
This causes problems if you have to distribute your exe on machines that have different Word versions.
I had this problem with a client. And as a developer you can't always tell your clients to upgrade to your latest version of Word or any other program you're using.