views:

39

answers:

2

Ok so here's the story.

I have an array of Assemblies which are initiated by [Assembly].LoadFrom() that load a number of DLL's. Upon initialising, a method called InitMain (from the DLL!) is called, which sets a number of variables outside of this method, like here:

Public Class Example
    Dim C As Integer = 0

    Public Sub InitMain()
        C = 50
    End Sub

    Public Sub Test()
        MsgBox("C = " & C)
    End Sub
End Class

If I call the method Test using that same array of Assemblies somewhere later in the main application (like pressing a button or something to trigger it), it will pop up a messagebox with that says: "C = 0"

Why is this? Is it fixable without any odd workarounds?

Thanks in advance!

A: 

So you need to call the initMain sub after instantiating your class Example and before calling sub Test?

Beth
The Example class is in a DLL.
Angelo Geels
(Which is then loaded through [Assembly].LoadFrom().)So basically yes.
Angelo Geels
+2  A: 

The methods InitMain and Test, and your variable C are all instance variables defined in your call Example. Each instance of the Example class with have their own copies of both methods and the variable.

I suspect that your code creates a new instance of the Example class and calls the InitMain method on that. Later you create a new instance and call Test on that. These two instances will not share the same copy of the variable C.

The workaround is to define C as a Shared (static in C#) variable. Now you will only have one copy of the C variable that is shared between all instances of the Example class.

You may also want to define the InitMain and Test methods as Shared. If you do so you will only have one instance of each method and must call them like this: Example.InitMain() instead of creating an instance and calling the method on that.

Rune Grimstad
Ahh, great! This fixed the problem! Thank you very much! :)
Angelo Geels