Results 1 to 21 of 21

Thread: Early or Late binding.

  1. #1

    Thread Starter
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478

    Early or Late binding.

    I'v seen different manners of creating objects and some use late and early in same function (my opinion).

    So can someone tell me what is early and what is late from the following examles.
    VB Code:
    1. Dim myC As MyCls.MyFun
    2. Set myC = CreateObject("MyCls.MyFun")
    3.  
    4. Dim myC As MyCls.MyFun
    5. Set myC = New MyCls.MyFun
    6.  
    7. Dim myC As Object
    8. Set myC = CreateObject("MyCls.MyFun")
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  2. #2
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456
    Once you refernce the DLL from project - > references, its all early binding, does not matter how you use it.

    For these two to work, you will have to reference the MyCls from references. this is called early binding
    --------------------------------------------------------------------------------
    Dim myC As MyCls.MyFun
    Set myC = CreateObject("MyCls.MyFun")

    Dim myC As MyCls.MyFun
    Set myC = New MyCls.MyFun
    --------------------------------------------------------------------------------

    while this will work even if you do not reference the MyCls from references. this is called late binding. However, even with this style if you reference MyCls from Project - > References it will be early binding then.
    --------------------------------------------------------------------------------
    Dim myC As Object
    Set myC = CreateObject("MyCls.MyFun")
    --------------------------------------------------------------------------------

  3. #3
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    techyspecy is just a little off here. Early or late binding is a matter of how the object is declared. Not how it is created. If you declare an object variable of a specific class it is early bound.
    VB Code:
    1. Dim myC As MyCls.MyFun 'early bound
    If the object is declared as a genric object, it is late bound.
    VB Code:
    1. Dim myC As Object 'late bound
    In general, a early bound object runs faster and is more efficeint. Check into MSDN, there is a ton of info there on this subject.

  4. #4
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    To sum up the above two posts:

    You have to set a reference to the DLL serving the objects inside your project. Also you have to declare an object variable to be of a specific class type than declaring simply as Object.

    To add my own: It's believed to be faster if you use:

    Code:
    Dim MyObj As FunnyObject
    Set MyObj = New FunnyObject
    than this:

    Code:
    Dim MyObj As New FunnyObject
    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  5. #5
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456
    Originally posted by MarkT
    techyspecy is just a little off here. Early or late binding is a matter of how the object is declared. Not how it is created. If you declare an object variable of a specific class it is early bound.
    VB Code:
    1. Dim myC As MyCls.MyFun 'early bound
    If the object is declared as a genric object, it is late bound.
    VB Code:
    1. Dim myC As Object 'late bound
    In general, a early bound object runs faster and is more efficeint. Check into MSDN, there is a ton of info there on this subject.
    Take my word pal, you are wrong.
    As soon as you reference a DLL in your project its early binding. Becuase compiler already knows what bindings you are using.

  6. #6
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456
    Originally posted by techyspecy
    Take my word pal, you are wrong.
    As soon as you reference a DLL in your project its early binding. Becuase compiler already knows what bindings you are using.
    Pure late binding is when you do not reference the DLL in your project and use it in this manner :

    dim a as object

    set a = createobject("ADODB.Recordset")

    this is late binding, cause compiler does not know in advance that ADO is referenced, only at a point when it encounters the createobject line it binds the ADO library dynamically to the project. and this is what is called late binding. Once you reference the Library then it does not matter how you create object off it. Its already packed up with your project and thus its called early binding. Declaring objects then becomes nothing more than a matter of choice. Some people use New some use CreateObject some still use Object Style. But it really does not matter after referencing the library. Sure there are some performace issue with each style.
    Last edited by techyspecy; Feb 21st, 2003 at 08:50 AM.

  7. #7
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    Originally posted by techyspecy
    Take my word pal, you are wrong.
    As soon as you reference a DLL in your project its early binding. Becuase compiler already knows what bindings you are using.
    Straight form MSDN. Do a search for "Speeding Object References"
    Object references are early-bound if they use object variables declared as variables of a specific class. Object references are late-bound if they use object variables declared as variables of the generic Object class. Object references that use early-bound variables usually run faster than those that use late-bound variables.

    For example, you could assign a reference to an Excel object to either of the following variables:

    Dim xlApp1 As Excel.Application
    Set xlApp1 = New Excel.Application

    Dim xlApp2 As Object
    Set xlApp2 = CreateObject("Excel.Application")

    Code that uses variable xlApp1 is early-bound and will execute faster than code that uses variable xlApp2, which is late-bound.

  8. #8
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456
    Originally posted by MarkT
    Straight form MSDN. Do a search for "Speeding Object References"
    Listen dude,

    this is absolutely right, but it never says once you make a reference from project menu - > references and still declare them as object it will be late bound. Once you make a reference its done.

  9. #9
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    If you have a reference to Excel and you use this syntax
    VB Code:
    1. Dim xlApp2 As Object
    2. Set xlApp2 = CreateObject("Excel.Application")

    It is not early bound because at compile time the xlApp2 object is not bound to any specific object. It is not until the set statement that the app know that xlApp2 it going to be bound to an Excel object.

    The bottom line is if at compile time the object is not bound to a specific class then the object is going to be late bound.

    In order for an object to be early bound you do need a reference set to it in your project but just setting a reference to it will not make it early bound.

  10. #10
    Fanatic Member jian2587's Avatar
    Join Date
    Aug 2000
    Location
    I bet u need a fusion powered shuttle to reach my place...
    Posts
    963
    I consider this as true late binding
    VB Code:
    1. Sub InvokeClientFunction(objClient As Object)
    2. MsgBox objClient.GetARandomNumber
    3. End Sub

    Whereby you can pass any object as long as it has
    GetARandomNumber function. Mostly used in interface inheritance,
    whereby as a server, you've no idea in advance of what kind of
    client is passed to you. Those clients may or may not inherit the
    standard client interface as used by the server.
    ASM,C,C++,BASIC,VB,JAVA,VBS,HTML,ASP,PHP,mySQL,VB.NET,MATLAB
    Programming is fun, but only if you're not on a tight deadline
    So I consider all those working engineers sad people

    VB FTP class
    3 page PHP crash course
    Crash Course on DX9 Managed with VB.NET covering basics till terrain creation

  11. #11

    Thread Starter
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    Originally posted by jian2587
    I consider this as true late binding
    VB Code:
    1. Sub InvokeClientFunction(objClient As Object)
    2. MsgBox objClient.GetARandomNumber
    3. End Sub

    Whereby you can pass any object as long as it has
    GetARandomNumber function. Mostly used in interface inheritance,
    whereby as a server, you've no idea in advance of what kind of
    client is passed to you. Those clients may or may not inherit the
    standard client interface as used by the server.
    This isn't my question and is certanly not an example of a late bound object.

    The passed object can be a late or early bound object cause it is certanly bounded to a class before it enters this sub else you would get a object or with block not set error.

    Way besides the question.
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  12. #12
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    Here is an other MSDN quote. Found under CreateObject Function.
    Declaring an object variable with the As Object clause creates a variable that can contain a reference to any type of object. However, access to the object through that variable is late bound; that is, the binding occurs when your program is run. To create an object variable that results in early binding, that is, binding when the program is compiled, declare the object variable with a specific class ID. For example, you can declare and create the following Microsoft Excel references:
    If you can find anywhere in MSDN that says that setting a reference to something in the project makes it early bound then please point me to where I can find it because that is against everything I know.

  13. #13

    Thread Starter
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    Originally posted by techyspecy
    Listen dude,

    this is absolutely right, but it never says once you make a reference from project menu - > references and still declare them as object it will be late bound. Once you make a reference its done.
    Ha you think so.

    So if i set a refernce to word and to excel and i'll code it like
    VB Code:
    1. Dim obj As Object
    2.  
    3. If dudeAsks = "Excel" Then
    4.    Set obj = CreateObject("Excel.Application")
    5. Else
    6.    Set obj = CreateObject("Word.Application")
    7. End If

    How could this be early bound , compiler won't know which class there will be used in this function.

    If it would be a coding standard in the office to have early bound object you should need to set the dim statement in the If structure.

    What will be done on compiling here then ??
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  14. #14
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456
    You people do not get it, do you ?

    If you set the reference and do this stuff, it won;t know the difference but at run time it will pick that reference automatically while in other case it will look into V-Table for the proper reference of the referenced class in your code.

  15. #15

    Thread Starter
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    early bound
    VB Code:
    1. Dim objEx As Excel.Application
    2.    Dim objAc As Access.Application
    3.    Dim objWo As Word.Application
    4.    
    5.    If dudeWants = "Excel" Then
    6.       Set objEx = New Excel.Application
    7.    ElseIf dudeWants = "Access" Then
    8.       Set objAc = New Access.Application
    9.    Else ' this dude needs word
    10.       Set objWo = New Word.Application
    11.    End If
    12.    
    13.    Set objEx = Nothing
    14.    Set objWo = Nothing
    15.    Set objAc = Nothing
    late bound
    VB Code:
    1. Dim obj As Object
    2.    
    3.    If dudeWants = "Excel" Then
    4.       Set obj = New Excel.Application
    5.    ElseIf dudeWants = "Access" Then
    6.       Set obj = New Access.Application
    7.    Else ' this dude needs word
    8.       Set obj = New Word.Application
    9.    End If
    10.    
    11.    Set obj = Nothing
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  16. #16
    Fanatic Member jian2587's Avatar
    Join Date
    Aug 2000
    Location
    I bet u need a fusion powered shuttle to reach my place...
    Posts
    963
    Originally posted by swatty
    This isn't my question and is certanly not an example of a late bound object.

    The passed object can be a late or early bound object cause it is certanly bounded to a class before it enters this sub else you would get a object or with block not set error.

    Way besides the question.
    Look, just an example, if we're to check out if it's any particular
    object we can with TypeOf but I know that's not the point
    here. What I mean is, the passed object from the client to the
    server is definitely an object not recognized by the server, but the
    server knows what it wants to invoke. If the client doesn't
    support the invoketion, well u know wut happens, but that's not
    the point. So since the server doesn't recognize the object yet it
    invokes a function of the object which it doesn't know in advance
    of wether it is legal or valid or not. This is really a late-binding.
    During this stage, the program performs some trickery here,
    resulting in slow performance if u're to use late-binding.
    ASM,C,C++,BASIC,VB,JAVA,VBS,HTML,ASP,PHP,mySQL,VB.NET,MATLAB
    Programming is fun, but only if you're not on a tight deadline
    So I consider all those working engineers sad people

    VB FTP class
    3 page PHP crash course
    Crash Course on DX9 Managed with VB.NET covering basics till terrain creation

  17. #17

    Thread Starter
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    Originally posted by techyspecy
    You people do not get it, do you ?

    If you set the reference and do this stuff, it won;t know the difference but at run time it will pick that reference automatically while in other case it will look into V-Table for the proper reference of the referenced class in your code.
    Sure you don't

    binding happens at compile time , your program is compiled before it is run.

    If the class type isn't known at compile time it will bind it at runtime so it is late bound.

    If it can pick up that reference or not it is late if it needs to be done at runtime.
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  18. #18

    Thread Starter
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478

    Back to the question

    Why do people write early bounded objects with createobject instead of New

    VB Code:
    1. ' i find this funny coding when you have the reference set already
    2. Dim myC As MyCls.MyFun
    3. Set myC = CreateObject("MyCls.MyFun")
    4.  
    5. 'if you use early binding then this is my approach
    6. Dim myC As MyCls.MyFun
    7. Set myC = New MyCls.MyFun
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  19. #19
    Fanatic Member jian2587's Avatar
    Join Date
    Aug 2000
    Location
    I bet u need a fusion powered shuttle to reach my place...
    Posts
    963
    Hehe that's a pretty question
    I favor New instead of CreateObject
    eventhough both perform same stuffs but at some point, the
    decision of choosing between these two will change the control-
    flow of the program altogether. Not sure about this, I guess it's
    something "portrayed" in an example I downloaded which
    involves multi-threading using ActiveX exe. I see that the author
    writes CreateObject instead of New when he/she wants to create
    a new object running in its own thread. Perhaps if using New
    the thing will run in its parent thread instead.

    Correct me if I'm wrong.
    ASM,C,C++,BASIC,VB,JAVA,VBS,HTML,ASP,PHP,mySQL,VB.NET,MATLAB
    Programming is fun, but only if you're not on a tight deadline
    So I consider all those working engineers sad people

    VB FTP class
    3 page PHP crash course
    Crash Course on DX9 Managed with VB.NET covering basics till terrain creation

  20. #20
    Addicted Member Sheppe's Avatar
    Join Date
    Sep 2002
    Location
    Kelowna, BC
    Posts
    245
    Originally posted by techyspecy
    You people do not get it, do you ?

    If you set the reference and do this stuff, it won;t know the difference but at run time it will pick that reference automatically while in other case it will look into V-Table for the proper reference of the referenced class in your code.
    Unless you declare an object variable as a specific type, it is not early bound.

    VB's compiler isn't so smart, ya see. Here's an example as to why:

    Dim X As Blah
    Set X = New Blah

    ^^^ This is faster than

    Dim X As New Blah

    The reason being, in the second instance the compiler isn't smart enough to know the life cycle of the object (X), so every time it is referenced it has to see if it has been instantiated. This is not the case in the first instance.

    Just a bit of trivia for the day.
    Last edited by Sheppe; Feb 21st, 2003 at 11:36 AM.
    [vbcode]
    On Error Goto Hell
    [/vbcode]
    Sheppe Pharis, MCSD
    Check out http://www.vb-faq.com
    Click here for access to the free Code-Express source code and component sharing network for VB6
    Want a better way to skin your .NET applications? Click here!

  21. #21
    Hyperactive Member rplcmint's Avatar
    Join Date
    Jan 2001
    Location
    Stockton, CA
    Posts
    333
    When you declare a variable as Object, Variant, Form or Control, Visual Basic cannot determine at compile time what sort of object reference the variable will contain. There for VB must use 'late binding' to determine at 'run time' whether the actual object has properties and methods you call using that variable.

    You are forcing the 'GetIDsOfNames' method in the IDispatch interface to return the DispID for that given member name.

    Whether you reference the library or not doesn't matter. If you don't use the referenced library, VB uses late binding to make the proper calls to run the program.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width