Results 1 to 8 of 8

Thread: What is better? CreateObject or Set?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 1999
    Location
    Prague, Czech Republic
    Posts
    350
    Hi! I am making a MS Internet Transfer Control in code and I would like to know what is the difference between

    Set Inet = CreateObject("InetCtls.Inet")

    and

    Set Inet = New InetCtlsObjects.Inet

    with adding reference to the control.

    ============================================================

    When I try to use commF = Inet.OpenURL("[address]", icByteArray), it returns commF(0 to -1), but the file exists. Where is the problem?

    Thanks,
    John

  2. #2
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    In answer to your first question.

    If you use "CreateObject" with no refernce to the dll that contains the inet control, then you will get late binding.

    If you add a refernce to the dll, and dim all the variables as the correct type, then you will get early binding.

    Early binding is quicker than late binding, as with late binding your app has to mnake sure that each of the properties / methods you have used actually exist. If you have early binding, this is all done when compiling, so the code runs that much quicker.
    Iain, thats with an i by the way!

  3. #3
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Plus, if you use CreateObject, you will not get the drop down box because it has no reference to dll. When I say drop down box, I mean like the properties and methods that comes with that application.

    Iain is right, Early binding is faster than Late binding.

    The only time I use CreateObject, is when I only want to save as form and don't want the VBP file.

    If you use GetObject on the other hand, you must reference as a Early Binding.
    Chemically Formulated As:
    Dr. Nitro

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    No, you will sertanly get the "drop down" even if you use CreateObject. You will not get it however if you use late bindings. CreateObject can be used without using late bindings.

    Here's an example that uses early bindings with CreateObject:
    Code:
    Private myObj As CMyClass
    
    Private Sub Form_Load()
        Set myObj = CreateObject("MyDll.CMyClass")
    End Sub
    This is early bindings because myObj is declared as a special object and not as the Object type.
    CreateObject is slower then using the New keyword though.
    Because when you use CreateObject your application has to look up the UUID key in the registry during run-time. While using the New keyword the UUID key is looked up during the compile of the app and is compiled into the EXE.

    Best regards

  5. #5
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Sorry!

    What I meant was if you did not reference the dll and do something like this, you will not get the drop down menu.

    Code:
       Dim xlApp As Object
       Set xlApp = CreateObject("Excel.Application")
       xlApp.Visible = True
       xlApp.Visible = True
    Chemically Formulated As:
    Dr. Nitro

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    I understood that that was what you meant. I just wanted to clarify that using CreateObject isn't nessasary the same as late binding.
    In your example you use late binding because you declare xlApp as object. You do need to use CreateObject with late bound objects though. The New keyword is only valid with early bound objects.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 1999
    Location
    Prague, Czech Republic
    Posts
    350
    If I want the dropmenus, I can add a reference to the DLL only temporaly and then remove it.

    I meaned some more essential reasons...

    For example. Is my program able to run if it has a reference to the dll, but the dll is not on the computer?
    (If I use CreateObject I can handle the error..)

  8. #8
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    You can catch the error even if you use the New keyword.
    The only requriments is that you have the COM object installed and registred on your computer when you compile the project.
    Code:
    'this use early binding and it will not
    'compile if CMyClass doesn't exist on the
    'computer that makes the compilation
    'The EXE will however run on a computer that
    'lacks the CMyClass object
    Private o As CMyClass
    
    Private Sub Form_Load()
        On Error Resume Next
        Set o = New CMyObject
        If Err Then
            'CMyObject is missing or is not registred proparly
        End If
    End Sub

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