Results 1 to 7 of 7

Thread: CreateObject Vs New?

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2003
    Posts
    54

    CreateObject Vs New?

    I am a new VB programmer. I would Like to know the difference between

    CreateObject function and New Keyword.

    Please tell me in detail.


    Thanks in advance.
    Suresh

  2. #2
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    New forces the compiler to verify the existence of the COM server file, and it binds the object at compile time. This is called early binding. It is faster because the overhead of object creation happens during compile & link.


    CreateObject binds the COM server during runtime. The compiler has no idea if the COM server even exists. This is called late binding. This is slower for the creation of the first object in the class than early binding. It is also more error-prone, because the VB program has no way to know if the external COM object exists until the program attempts to create the object.

    Suggestion : get and read VB COM by Lewis Thomas

  3. #3
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253
    Don't forget that 'New' will use the VB runtime for all marshalling and transactional contexts if the class is in the same VB Project whilst CreateObject will always use SCM for this regardless of class location.

  4. #4
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253
    CreateObject binds the COM server during runtime. The compiler has no idea if the COM server even exists. This is called late binding. This is slower for the creation of the first object in the class than early binding. It is also more error-prone, because the VB program has no way to know if the external COM object exists until the program attempts to create the object.
    Not strictly true: If you declare (Dim) the objects IUnknown interface (or IDispatch if you have to) and then use CreateObject you will use early binding.

    Create Object is slower because it performs a registry lookup of the ProgId, and then uses the SCM to instantiate the object.

  5. #5
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Y: You're correct-- but this guy is new - assume he doesn't know IUknown from IDispatch from a vtable.

  6. #6
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253
    ok

  7. #7

    Thread Starter
    Member
    Join Date
    Apr 2003
    Posts
    54

    Thank You

    Thank You very much.
    Suresh

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