Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

CreateObject vs New

CreateObject vs New

CreateObject vs New

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.
Compta, S.A.

RE: CreateObject vs New

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...


RE: CreateObject vs New

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

RE: CreateObject vs New

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!

The more I learn, the more I need to learn!

RE: CreateObject vs New

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.

RE: CreateObject vs New

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


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close