Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CreateObject vs New

Status
Not open for further replies.

BML

Programmer
Oct 24, 2000
37
PT
I have a application that uses MTS (on Server). The Clients are made in VB and use the components made in VC++. I'm having real doubts on the way I create my objects in VB:

Can anyone tell me the diferences between the use of CreateObject and the use of New operator(or use variable as New).

I'm interested particularity in the fact of specifing a server in CreateObject function. That means that I don't have to register the Dll's locally ?

Are diferences in performance ? or in stability of tha app ?

Bruno Loureiro.
brunoloureiro@usa.net
Compta, S.A.

 
Per Steve Smith, an MTS guru:

Your choice of method depends upon the nature of the object you are creating.

If it is an MTS object itself, which needs to participate in the context of the current object, then you need to use CreateInstance. This is true even if the object is in the same DLL as the calling function.

Use NEW to instantiate non-MTS objects, such as ADO objects. The difference between New and CreateObject in this case is that New will create an instance of a class based on your project references and CreateObject lets you specify a specific ProgID. New does not use COM, whereas CreateObject does.

The New keyword should also be avoided in COM+ components. Use CreateObject instead.

Hope this helps...

Tom
 
Use New for non MTS objects.
Use CreateObject for creating MTS objects from programs outside of MTS
Use CreateInstance for creating MTS objects from within other MTS objects
 

Tom and Karl are both correct.

I used to use the New keyword for all my object creation and the package would crash every few days with a message about "Context Wrapper". It turns out that when you create MTS objects using the New keyword, the MTS engine does not know anything about them; thus, it never cleans up after these objects which eventually leads to memory leaks and unexpected behavior.

A really good book on MTS and VB is "Professional Visual Basic 6 MTS Programming" by Wrox, author Matthew BortNiker. It was recommended to me by a Microsoft engineer and it was a great recommendation.

Hope this helps!


Tarek

The more I learn, the more I need to learn!
 
Can I second what Tarek has said about "Professional Visual Basic 6 MTS Programming".

I cannot recommend the book enough. It covers MTS very well.
 
The New keyword in VB is used to create a new instance of a class as an object, for use within the current application. When used in a component within MTS, it creates a Private instance of an object that MTS knows nothing about. It won't have its own context object, and won't be included within the current transaction. In other words, it has to look after itself with no help from MTS.

The CreateObject method is almost exactly the opposite. When used in a component running within MTS, this creates a new instance of an object that MTS will treat as separate from the current component instance—and it will get its own context object. However, this will not contain any information from the context of the component that created the new instance, so it will run outside the current transaction.

The CreateInstance method of the context object provides a solution to these two problems. It creates a new instance of the referenced component and provides it with a new context object. However, it also copies the transaction information from the context of the object that created it into the new context—thus making it part of the current transaction
Regards,
Anbu.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top