COM object preserve values between invocations

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.
LVL 40
Vadim RappAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Fernando SotoRetiredCommented:
We need to see all the code where this is happening in. The couple of lines above does not show us much.
Vadim RappAuthor Commented:
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 SotoRetiredCommented:
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.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Vadim RappAuthor Commented:
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?
Fernando SotoRetiredCommented:
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

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
Vadim RappAuthor Commented:
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 SotoRetiredCommented:
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.
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
.NET Programming

From novice to tech pro — start learning today.