By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,344 Members | 1,114 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,344 IT Pros & Developers. It's quick & easy.

Module Scope between instance of the same class

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Frederic H wrote:
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!

--
Larry Lard
Replies to group please

Nov 21 '05 #2

P: n/a
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 indeed, they are global in the application but intra objects... It's
like a Shared variable in VB.NET...
Nov 21 '05 #3

P: n/a
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
"Frederic H" wrote:
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 indeed, they are global in the application but intra objects... It's
like a Shared variable in VB.NET...

Nov 21 '05 #4

P: n/a
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 ?
Nov 21 '05 #5

P: n/a
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
"Frederic H" wrote:
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 ?

Nov 21 '05 #6

P: n/a
On 2005-09-29, Frederic H <Fr*******@discussions.microsoft.com> wrote:
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?
Nov 21 '05 #7

P: n/a
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 :
On 2005-09-29, Frederic H <Fr*******@discussions.microsoft.com> wrote:
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?

Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.