473,398 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Module Scope between instance of the same class

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
7 1368
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Hal Vaughan | last post by:
Being self taught, this is one thing I've always had trouble with -- I finally get it straight in one situation and I find I'm not sure about another. I have a class that keeps calling an...
2
by: Jeff Epler | last post by:
Hello. Recently, Generator Comprehensions were mentioned again on python-list. I have written an implementation for the compiler module. To try it out, however, you must be able to rebuild...
0
by: The Dark Seraph | last post by:
I have a singleton object that works perfectly from inside my main .py file. However, when I want to access the object that backs the singleton from another file ( either from an execfile() or...
2
by: Ashish Shridharan | last post by:
Hi All I have been tryign to figure out the scope of a module as shown declared below in a web environment. My Questions are Is this module shared across multiple instances of the...
4
by: Michael Maes | last post by:
Hello, ° If you declare a Module (Shared Class) like: Namespace Global.Structs Module Credentials Private _UserFullName As String Public ReadOnly Property UserFullName() As String Get...
26
by: Patient Guy | last post by:
The code below shows the familiar way of restricting a function to be a method of a constructed object: function aConstructor(arg) { if (typeof(arg) == "undefined") return (null);...
9
by: Rudy | last post by:
Hello All! I'm a little confused on Public Class or Modules. Say I have a this on form "A" Public Sub Subtract() Dim Invoice As Decimal Dim Wage As Decimal Static PO As Decimal Invoice =...
7
by: John Salerno | last post by:
I have the following code: class DataAccessFrame(wx.Frame): menu_items = def __init__(self): wx.Frame.__init__(self, None, title='Database Access Panel')
7
by: surfrat_ | last post by:
Hi, My project has about 4 source files (xxx.cs) and I am having a problem with scope between the files. If I put the code all within one class everything works OK. Can you please point me to...
3
by: johnny | last post by:
Can a class inside a module, access a method, outside of class, but inside of the module? Eg. Can instance of class a access main, if so how? What is the scope of "def main()" interms of class...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.