Never use New to create MTS/COM+ objects

A common question among VB developers is: Why is it dangerous the use of the New keyword in VB to create COM objects registered under MTS/COM+?

The New keyword is the only way to go when you want to create objects that are defined as PublicNotCreatable or Private (CreateObject doesn’t work) inside the same component as the calling code. As you know VB lets you use New to create Multiuse classes as well. Unfortunately the New keyword has a couple of drawbacks:

1) In situations where the caller and the callee are in the same EXE/DLL COM component VB perform an internal creation of the COM object bypassing the COM run-time (and the MTS run-time if the object is registered under MTS). While this fact is not a problem under a non-MTS environment, the situation gets very dangerous under MTS and COM+. What happens is that, since MTS/COM+ is not notified of the object creation, the newly created object gets access to the ObjectContext of the father and MTS sees the code running inside method calls of the child object as “inline-code” of the father. I leave as an exercise to the reader what can happen if the child object calls SetComplete (against the ObjectContext of the father).

2) When you declare an object (Interface) with the syntax

Dim x As New MyClass

VB checks every time the object is referenced in the code. If it is Nothing, VB silently (re)creates it (This means you’ll never get a “object variable or with block not set” error). If you are not aware of this your code could follow unexpected execution paths.

Public sub DoWork()        Dim X as New MyClass   ' MyClass is registered as "requires transaction"  x.dosomemorework       ' VB creates it, the component ball start spinning in                          ' the MTS explorer, the transaction start  Set x = Nothing        ' The ball stop spinning  If x Is Nothing Then   ' VB recreates it, the ball start spinning again, the                          ' transaction start again      Format Drive C     '  this code will never be executed  End If       ' From here to when x goes out of scope x is activated   ' (and probably you don't know this).  End Sub    

This tip is taken from the FAQ list that Enrico mantains at his own home page (

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS