Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Enterprise
Expertise: Intermediate
Jul 23, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Use SafeRef() Before Passing a Reference of an MTS Object

SafeRef() returns a reference to the context wrapper instead of a reference to the object itself. You should never pass a direct reference of an MTS object to a client application. If you do, the client application can make a call on the MTS object without going through the context wrapper. This defeats the interception scheme set up by the MTS run time. You should use SafeRef() function which returns the outside world's view of an MTS object.

Let's say that you're writing a method implementation for an MTS object. In the method, you want to create an object of some class and then pass your own reference to the newly created object as follows:

Dim pSomeClass As CSomeClass
Set pSomeClass = New CSomeClass

pSomeClass.SomeMethod Me                 ' Incorrect code

This code is incorrect under MTS, however. The child object (pSomeClass) can invoke method calls on your object without going through the context wrapper. You should never bypass the interception scheme set up by the MTS run time. Instead, you should pass a reference to your object as follows:

pSomeClass.SomeMethod SafeRef(Me)        ' Correct code

By calling SafeRef, you allow the child object to establish a connection that passes through the context wrapper. This keeps things in line with what the MTS run time expects. Note that the Me keyword is the only valid parameter you can pass when calling SafeRef with Visual Basic.

Deepak Pant
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date