Losbear Losbear - 1 year ago 141
Vb.net Question

vb.net CreateObject vs direct reference using New

I have a large .net solution that has a lot of projects in it.
My question is, when referencing one project from another, is it better to do this:

Dim objSomething As Project.Class1 = Nothing
objSomething = CreateObject("Project.Class1")

Or simply:

Dim objSomething As New Project.Class1()

Also, is there a performance hit on one over the other?
Thanks in advance

Answer Source

You'd have to try it both ways and measure it to be sure, but there should not be any significant performance difference if Class is a pure COM-object.

Other than that, there's no difference since objSomething is typed as a Project.Class1 in both cases so everything after that is early-bound.

If, however, Project.Class1 is a .NET object that is com-callable, then you should ALWAYS use New because you bypass the COM-interop layer which WILL have a performance impact. There's no point in creating a .NET class through a COM interop layer.