Module Scope between instance of the same class

G

Guest

Hi all,

If someone as an explanation ... :) Look at this...

Class MultithreadTest
' <--------------
Option Explicit
Public Sub Initialize()
Nbr = 0
End Sub
Public Function GetNbr() As Integer
GetNbr = Nbr
End Function
Public Sub Add()
Nbr = Nbr + 1
End Sub
' -------------->

Module.bas
' <--------------
Public Nbr As Integer
' -------------->

Application
' <--------------
Private Sub Command1_Click()
Dim obj1 As New Srv.MultithreadTest
Dim obj2 As New Srv.MultithreadTest
obj1.Add
obj1.Add
obj1.Add
obj1.Add
obj1.Add
obj1.Add
MsgBox "OBJ1 " & obj1.GetNbr & " ---- " & "OBJ2 " & obj2.GetNbr
Set obj1 = Nothing
Set obj2 = Nothing
End Sub
' -------------->

Result : "OBJ1 6 ---- OBJ2 6"

Should be "OBJ1 6 ---- OBJ2 0" ?
Bug ?
Shared variables in module between objects ?


Thx for your help
 
L

Larry Lard

Frederic said:
Hi all,

If someone as an explanation ... :) Look at this...

Class MultithreadTest
' <--------------
Option Explicit
Public Sub Initialize()
Nbr = 0
End Sub
Public Function GetNbr() As Integer
GetNbr = Nbr
End Function
Public Sub Add()
Nbr = Nbr + 1
End Sub
' -------------->

Module.bas
' <--------------
Public Nbr As Integer
' -------------->

Application
' <--------------
Private Sub Command1_Click()
Dim obj1 As New Srv.MultithreadTest
Dim obj2 As New Srv.MultithreadTest
obj1.Add
obj1.Add
obj1.Add
obj1.Add
obj1.Add
obj1.Add
MsgBox "OBJ1 " & obj1.GetNbr & " ---- " & "OBJ2 " & obj2.GetNbr
Set obj1 = Nothing
Set obj2 = Nothing
End Sub
' -------------->

Result : "OBJ1 6 ---- OBJ2 6"

Should be "OBJ1 6 ---- OBJ2 0" ?

Why would it be that?
Bug ?
No.

Shared variables in module between objects ?

Yes. Variables declared in modules are global to the whole application.
If you want class variables then declare them in the class!
 
G

Guest

Yes. Variables declared in modules are global to the whole application.
If you want class variables then declare them in the class!

Yes indeed, they are global in the application but intra objects... It's
like a Shared variable in VB.NET...
 
G

Guest

All module methods and variables are implicitly shared.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
Clear VB: Cleans up outdated VB.NET code
 
G

Guest

Ok thanks.Well, Bad news for me ...
Is it possible to explicitly put the variable as "NOT" shared or the
unique solution is to convert modules to classes ?
 
G

Guest

You'll need to put the variable into a class to have it be instance specific.
(modules' members are always shared)
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
Clear VB: Cleans up outdated VB.NET code
 
D

david

Ok thanks.Well, Bad news for me ...
Is it possible to explicitly put the variable as "NOT" shared or the
unique solution is to convert modules to classes ?

The real question is if you want each instance of MultiThreadClass to
get its own copy of Nbr, why aren't you defining it as a class variable
within MultiThreadClass.

And just to be snarky (but serious), why not "Number" instead of Nbr?
 
G

Guest

Sure, in POO it's like that. But this COM DLL is developped with modules...
Global variables are delcared on modules <= really bad idea.

When I use multithreading I have one instance of the DLL on each thread.
Then you understand that DLL dont give the good result :)

I will try to convert DLL code in a TRUE object oriented code...

Thanks for your help !

"david" a écrit :
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top