Instantiate COM+ using New keyword


I've already learned that when doing programming for a COM+ class we must get rid of New keyword if we need to instantiate another object hosted in COM+. However, is it allowed to instantiate a class hosted in COM+ using New keyword from VB client? Let's say I've got a Foo class hosted in COM+ then is it ok to instantiate it in my VB Form like this:

         Dim objFoo as MyComponent.cFoo '// cFoo is a COM+ hosted class
         set objFoo = new cFoo

or should I instantiate it using CreateObject?

         Dim objFoo as MyComponent.cFoo
         set objFoo = CreateObject("MyComponent.cFoo")

Which one is better and safest?

Thanks for any help

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try first to do it with New (they are nearly equal)
Naveen SwamyYash Infinite Solutions Private LimitedCommented:
copied from msdn

You approach building MTS components differently than components for stand-alone applications. You need to understand how MTS initializes the components and how you tell MTS that you’re done with the component. First, you can’t create an MTS object by using the New keyword. When you use New (for example, Set myObj = New obj), VB bypasses MTS and provides an object pointer that points directly to the new object. To create an MTS object, you can use either the Server.CreateObject method, which MTS will intercept, or you can use the ObjectContext.CreateInstance method. If you already have an ObjectContext reference, use CreateInstance.

The object reference that you create via CreateObject or CreateInstance isn’t a direct pointer to the object at all (although your program can’t tell the difference). Instead, it’s a pointer to a context object, which MTS creates as an intermediary. The context object’s job is to fool your program into thinking that it can control the target object directly. Therefore, you can treat the object as if you had a “real” object pointer. Behind the scenes, MTS manages one or more instances of the object, but those few instances may be connected to many clients.

Whenever you call a method of your object, MTS looks for the first free instance and passes the call to that instance. That means that the instance that handles your second method call may not be the same instance that handled the first. At this point, you should begin to see why MTS improves scalability. If there were five clients, and each needed to use a certain component, you would normally have five copies of the component in memory. With MTS though, you might only need one or two. MTS uses the time between method calls to recycle a component for many clients.

Because many clients share the components, you can’t set a property on a component and then run a method that depends on the component “remembering” the property. For example (and I promised in Chapter 11 that I’d tell you about this), if you set a database access component’s ConnectionString property, then use that property to make multiple method calls to the database, the property may not be available. That’s the primary reason that I set up the CRepositoryData class so that you have to pass the ConnectionString with each method call—so it would be easier to move that class into MTS.

A client program can use an MTS object pointer over many method calls and everything will work fine, but the component needs to inform MTS each time it completes a unit of work so that MTS can recycle it. Components do this via the ObjectContext object. Here’s a typical sequence:

The client requests an MTS component instance—an object.
MTS provides a handle to an ObjectContext object instead.
The client accepts that handle as a “real” object reference.
The client makes a method call to the component.
MTS locates an idle instance of the component. If none are available, it creates a new instance.
MTS passes the method call to the component.
The component uses the getObjectContext method to obtain a reference to its object context.
The component performs the method. If the method is successful, the component calls the ObjectContext.SetComplete method; if the method is unsuccessful, it calls ObjectContext.SetAbort.
In either case, success or failure, the call to SetComplete or SetAbort informs MTS that the component has completed its work and is ready to be recycled.
When the client calls another method on the component, the cycle begins from step 5, not from step 1. A different instance of the component is likely to perform the method.

hope this helps u
Naveen SwamyYash Infinite Solutions Private LimitedCommented:
   '******ADO objects are not created using ObjectContext.CreateInstance but rather the New
    '******operators - this is because ADO objects do not need to be created using
    '******ObjectContext.CreateInstance to participate in the current transaction (if any).
    '******If you are using a custom wrapper for database access (which in turn could be using ADO) you
    '******would need to create this wrapper using ObjectContext.CreateInstance for it to participate in the MTS transactions.
    '******reference (;EN-US;234218)
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Naveen SwamyYash Infinite Solutions Private LimitedCommented:

'******CreateObject and CreateInstance Have the Same Effect in COM+
MiBlgAuthor Commented:
navstar16, you're close to get me to the answer, but let me have one more question. If I create a VB Form which calls COM+ object, it's actually in different process from the COM+ object so am I right to think that I can instantiate it using New keyword in order to get fastest performance in object creation?


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Hi MiBlg,
This old question (QID 20562109) needs to be finalized -- accept an answer, split points, or get a refund.  Please see for information and options.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.