Click to See Complete Forum and Search --> : CreateObject Vs New?
Suresh_Sajja
Aug 31st, 2003, 11:34 PM
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.
jim mcnamara
Sep 2nd, 2003, 09:49 AM
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
yrwyddfa
Sep 2nd, 2003, 11:14 AM
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.
yrwyddfa
Sep 2nd, 2003, 11:16 AM
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.
jim mcnamara
Sep 2nd, 2003, 02:52 PM
Y: You're correct-- but this guy is new - assume he doesn't know IUknown from IDispatch from a vtable.
yrwyddfa
Sep 3rd, 2003, 01:43 AM
ok
Suresh_Sajja
Sep 5th, 2003, 07:44 AM
Thank You very much.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.