Module Scope between instance of the same class

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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!
 
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...
 
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 ?
 
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
 
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?
 
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 :
 
Back
Top