COM object preserve values between invocations

Vadim Rapp
Vadim Rapp used Ask the Experts™
in VB 2008 I create managed com object, which I then call from an application. The application runs vbscript* where it's using CreateObject for my object.

In COM object I have a Module with Friend variable, which value is set at the execution time.

If I then run the script 2nd time, I found that the variable in the COM object already has previous value. I expected that CreateObject would re-initialize it.

run the script
   set M = createobject("myapp.myobject")
                                                                                 Friend X as string
                                                                                 X = "myvalue"
  Set M = Nothing

run the script again
   set M = createobject("myapp.myobject")
                                                                                 Friend X as string

At this point, X in the object will already have "Myvalue".

Does anybody know where is this behavior documented? I expected that "createobject" would indeed create new object from scratch, rather than reuse the old one; especially after prior Set M = Nothing

*) The application is using its own script engine based on vbscript. Microsoft used to license VBscript engine to 3rd parties, so they incorporated it in their own product. This is one of them.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fernando SotoRetired
Distinguished Expert 2017

We need to see all the code where this is happening in. The couple of lines above does not show us much.
Here's sample project. Build, then run tryme.vbs, and it will return the value 13 set in the previous life.

Don't forget to "clean" in the end of the experiment.
Fernando SotoRetired
Distinguished Expert 2017

I have not worked with COM or built any.

I built the .Net app and ran the tryme.vbs and it gave an exception of Could Not Create COM Object.

From looking at the .Net code you placed the lMyVar variable in a Module which makes lMyVar Shared, meaning that all users of this code use this one value. I suspect that you are using this variable as the reference counter for the object. Because this is manage code it does not get removed from the GC immediately and therefore when you make the second call you are picking up the old object and most likely the reason why you're getting the same value.  

I do not know how much I can be with this question.
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

Actually, I see the same even without COM object. Even if this is a usual class, the code below shows 3:

        Dim o1 As New ClassLibrary1.Class1
        o1.myvar = 3
        o1 = Nothing

        Dim o2 As New ClassLibrary1.Class1

So, how to ensure that this does not happen and when I create new instance, it's not reusing the old copy?
Distinguished Expert 2017
That is because IMyVar is Share throughout the Com project. Delete it from Module1 and place it in ComClass1 as shown below.

    Friend lMyVar As Integer

    Public Property MyVar() As Integer
            Return lMyVar
        End Get
        Set(ByVal value As Integer)
            lMyVar = value
        End Set
    End Property

Open in new window

I see, thanks. Can you explain, or refer me to some reading where this is described, what's the difference and why it works this way?
Fernando SotoRetired
Distinguished Expert 2017
In Visual Basic when you place variables in a Module they are available everywhere with that assembly. They come into existence when the application starts and remain defined until the application terminates. So when you crated the first COM instance in the application and you initialized the IMyVal to 3 and then you made the COM object that held the instance to Nothing  IMyVal still exist it does not go away because it is a Share'ed variable. So when you created a new instance of the COM object IMyVar still has its last value.

Microsoft Documentation on the Module.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial