Late & Early Binding

Discussion in 'ASP .Net' started by RN1, Mar 18, 2008.

  1. RN1

    RN1 Guest

    Is this late binding?

    --------------------------------------------------------------------------------
    Dim dSet As DataSet
    dSet = New DataSet
    --------------------------------------------------------------------------------

    & is this early binding?

    --------------------------------------------------------------------------------
    Dim dSet As New DataSet
    --------------------------------------------------------------------------------

    Which one should a developer strive for & why? Also what are the
    advantages & disadvantages of one over the other?

    Thanks,

    Ron
    RN1, Mar 18, 2008
    #1
    1. Advertising

  2. "RN1" <> wrote in message
    news:...
    > Is this late binding?
    >
    > --------------------------------------------------------------------------

    ------
    > Dim dSet As DataSet
    > dSet = New DataSet
    > --------------------------------------------------------------------------

    ------
    >
    > & is this early binding?
    >
    > --------------------------------------------------------------------------

    ------
    > Dim dSet As New DataSet
    > --------------------------------------------------------------------------

    ------
    >
    > Which one should a developer strive for & why? Also what are the
    > advantages & disadvantages of one over the other?


    I think you are confusing Classic VB6 issues with ASP.NET.

    In VB6 NGs many moons ago this question would arise on a regular basis
    because the OP is confused about what the difference is between late and
    early binding.

    In .NET these terms are not used although there are some issues that are
    similar. In your above code the two approachs are identical and neither
    relates to any form of binding.

    Binding occurs when a method or property is accessed. Take this:-

    Dim o As MyClass
    Set o = New MyClass
    o.Method()

    In VB6 the call to method is early bound (also known as vtable bound) since
    at compile time the compiler can determine where the pointer to the method
    function will be. OTH,

    Dim o As Object
    Set o = New MyClass
    o.Method()

    In VB6 the call to method is late bound since at compile time the compiler
    can't determine where the method function pointer will be all it knows is it
    has an object that has a method function. At runtime the code has to query
    the IDispatch interface for a DispID for the "method" member then dispatch a
    call to it via the IDispatch interface. Much slower.

    This business with the inline new in the Dim line was very often quoted as
    being related to binding but was in fact an entirely different issue
    altogether.

    Dim o As MyClass
    o = New MyClass
    o.Method()

    In VB.NET the compiler can resolve exactly where the method is. In fact the
    JIT native code compiler can go further and possibly remove the method call
    altogether by inlining the code in the method.

    Dim o As Object
    o = New MyClass
    o.Method()

    On the face of it the compiler can't know where the Method function is at
    compile time and has to leave resolution of it to runtime. However at
    runtime things are slicker since the full type information is available to
    it and it can resolve where method is far quicker. Hence this isn't really
    like the old VB6 late binding where its possible to call a method of an
    object whose type the runtime knows nothing about.

    A new variation of this is introduced to the language when a method is
    marked as overridable (virtual is C# parlance). In this case the compiler
    can't know where the method function is even for a variable typed as MyClass
    since it may hold a reference to a sub-class that has overriden the method.

    Note in these simplistic cases the compiler and/or JIT compiler can probably
    resolve stuff more inteligently but the principle holds.


    --
    Anthony Jones - MVP ASP/ASP.NET
    Anthony Jones, Mar 19, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ThunderMusic

    Plugins and Late Binding

    ThunderMusic, Jan 27, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    834
    Patrice
    Apr 12, 2005
  2. shaun.86

    early vs late binding

    shaun.86, Dec 19, 2005, in forum: Java
    Replies:
    2
    Views:
    16,455
  3. Stu
    Replies:
    1
    Views:
    579
    Arne Adams
    Dec 18, 2004
  4. Olaf Meding

    MS COM early and late binding

    Olaf Meding, Apr 27, 2004, in forum: Python
    Replies:
    4
    Views:
    383
    Simon Brunning
    Apr 28, 2004
  5. Replies:
    2
    Views:
    838
    Kevin Grover
    Oct 20, 2006
Loading...

Share This Page