Results 1 to 10 of 20

Thread: Testies external shared Libraries, regedit, registry

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10

    Referring/ making available the "VBA available Libraries". Late and Early Binding

    Referring/ making available the "VBA available Libraries". Late and Early Binding.


    The terms Early and Late Binding are often heard in connection with making available the “VBA available Libraries”.
    In this post I am comparing those and explaining 4 ways to do Binding, 2 using Early and 2 using Late.

    Because of the nature of the “direct link libraries” principle there is not a major difference and in most cases theses two basic ways can be used interchangeably.
    A final “product” of Binding, a final goal, is to have a variable object available in the code which has the Methods and Properties available in the usual object orientated programming way:

    ___ ThatLibraryObject.StufffromItIWant

    There are several ways to achieve this, some using the Early and some using the Late as well as a pseudo “Laterly Early” “Binding” way. I am discussing the 4 common ways, 2 using Early and 2 using late
    Early and Late Binding General Considerastiions: Early V Late Binding
    The main difference in the definition here lies in how/when the object is created.

    1) Early Binding
    Early Binding will require the ….” .. -- VB Editor -- Tools ---- references --- scroll down and check the appropriate library from the massive list of those available ..”… stuff
    1 Tools 2 referrences 3 Scroll Find Check.JPG . https://imgur.com/jyLjdBb
    Attachment 2001

    Early somehow makes the library links “known” to VBA before any code writing is done. The most noticeable distinguishing characteristics are then that a variable can be declared ( Dim’d ) as an object from that library, and that intellisense will work thereafter.
    Apparently this way makes a code faster than the Late Binding way, but I expect other factors concerning the implementation may influence this. So using Early Binding will result in the complied code effectively having a “complied link” section.
    A variable must generally be used to assign the object type to. This could be chosen to be of Object type , but intellisense will not work and then it effectively becomes a form of Late Binding: Sometimes the definition is contested by some, but generally the documentation says words to the effect that when a variable is declared to be of a specific object type, the compiler allocates memory and perform other optimizations before an application executes. The various dynamic help including intellisense is not available for a declaration of Object type. This is then the definition and characteristics of early Binding.

    Early Binding 1)(a) Set way. In the simplest form of Early Binding we would do something like this in our code, pseudo
    Dim myThing As SpecificClassOrObject
    _ Set myThing = NewInstance of SpecificClassOrObject

    The first line effectively makes the variable of the type required, and the second effectively fills the variable creating an actual instance of it. Instance in this respect means that everything possibly with the thing is made available. The Dim is done at compile and so Excel VBA Knows about / is aware of it allowing intellisense. The Set effectively “makes it live.”

    Early Binding 1)(b) Auto instancing Early binding “Way Dim ( using an auto instancing variable )
    Dim myThing As New SpecificClassOrObject
    Excel VBA knows about the type once again. But the instance has not been created. VBA is written generally in such a way that if it “knows” about a type but does not have it instance , it
    _(i) has no record of it being instanced, will always check on encountering the variable,
    and
    _(ii) if it has not been instance then it will do so. ( But nether the less on encountering it will still check every time.
    This “Way Dim” may make a code run a little slower.


    The main disadvantage of the Early Binding are in sharing when you cannot control what is checked in the library list of the recipient. Indeed they may have a version of Office that either does not have the library or may have a different version.
    ( In general Excel will update a recognised library to a newer version but not always the other way around, at least for libraries other than the main ones. )

    2) Late Binding
    The main distinguishing characteristic of the Late binding is that a function , ( CreateObject(“ “) ) , ( sometimes referred to as a method ) is used at run time to return the necessary reference to the object. So in the simplest form we have, if using a variable , we have Pseudo

    Late Binding 2a) Variable way.
    Dim myThing As Object
    _ Set myThing = CreateObject(“SpecificClassOrObject”,”ServerIfNotThisComputer”)

    or like more commonly seen
    Dim myThing As Object
    _ Set myThing = CreateObject(“LibraryName.ObjectfromItIWant__”)

    As the argument is taken as a string, it is, as is usual in compilation, ignored. The compilation can then have no knowledge of the object type.
    In terms close to what happens is that windows has an entry in a registry that associates the string with the dll that implements this object. Sometimes larger things don’t install named class entries in the registry for its objects. The CreateObject(“ “) will also take its unique Class ID (CLSID) if you know it , in this form , for example for a data object, CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")

    The usual disadvantage of Late Binding given is the lack of intellisense.
    The usual advantage given is not having to have the appropriate library in the reference list checked.
    A further feature, debatably an advantage of sorts is that the function can be embedded in a code line in a typical Object Orientated Hierarchy way such that no object variable for the main object must be used, pseudo

    Late Binding 2b) With__End With way
    _ PropertyOrXvalue = CreateObject(“LibraryName.ObjectfromItIWant__”).ThatPropertormethodOfObject(PossiblyArgumentsEtc)
    ' or
    __With CreateObject(“LibraryName.ObjectfromItIWant__”)
    ___PropertyOrXvalue = .ThatPropertormethodOfObject(PossiblyArgumentsEtc)
    __End With
    Code:
     
      ' Late Binding 2b) With__End With way
    
      PropertyOrXvalue =  CreateObject(“LibraryName.ObjectfromItIWant__”).ThatPropertormethodOfObject(PossiblyArgumentsEtc)
    
    ' or
    
        With CreateObject(“LibraryName.ObjectfromItIWant__”)
         PropertyOrXvalue = .ThatPropertormethodOfObject(PossiblyArgumentsEtc)
    
    
        End With
    _.____


    Available object constant variables in Early Binding
    A final minor consideration in the comparison of the Early and Late Binding concerns some simple constant variables. Some libraries contain some of these. Using Early Binding these are available. As the compilation has no knowledge of the object type in Late Binding, those variables are not “put in” the Object variable. These constants are usually simple numbers, and I personally would tend to use the literal numbers in a code anyway, be it Early or Late Blinded.
    Sometimes the names of the variable can give an indication of what they are, but I would tend to write the variable name of any I use and possibly further explanations of it in the ' comments

    Scope Issues and Early and late Binding, Public Pubics.
    In all ways that use a variable, that is to say all the Early Binding ways, 1a) 1b) and the first shown way of Late Binding 2a) , the declare, ( Dim ) , statement could be inside a routine or at the beginning of a Module.
    If it were at the beginning of a normal code Module, then the object would be available in all codes which would be defined as Pubic scope.
    If it were within a routine, then the object would be available in that routine which would be defined as Private scope.
    The last shown ways of Late Binding, 2b) , which do not use a variable can only be Private scope as it is not possible to have a the outside the CreateObject(“ “) outside of a routine.

    ( Note in passing that within a Class module we can pseudo have the Pubic scope by declaring as
    Pubic myThing = xxxxx
    This has the effect of making our object an object in that Class. It can then be referenced in all codes , for example if it is in the ThisWorkbook class module by
    __ ThisWorkbook.myThing
    Technically this may not be a Pubic variable, but to all practical intents and purposes it is . I have found some novel uses of it actually as you can use sometimes something like Sheet1.myThing in a code which exposes the interface and sets something off in a way that is not always possible with a true Pubic variable.
    Attached Images Attached Images

Similar Threads

  1. Replies: 23
    Last Post: 04-10-2025, 01:36 AM
  2. Replies: 9
    Last Post: 05-13-2021, 02:31 PM
  3. Testies
    By sandy666 in forum Test Area
    Replies: 0
    Last Post: 05-27-2020, 06:10 AM
  4. Combobox Not Working In Excel Workbook Shared Mode
    By peter renton in forum Excel Help
    Replies: 15
    Last Post: 06-03-2013, 01:25 PM
  5. Get External Data Error
    By marreco in forum Excel Help
    Replies: 2
    Last Post: 01-05-2013, 08:20 PM

Posting Permissions

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