Shared Public Variables and Shared Methods

J

Joe Fallon

I have a Shared varibale in a base class and all the Shared methods in the
sub-classes use it (and CHANGE it).
I thought I was saving myself the "trouble" of Dimming this variable inside
each Shared method.
But now I wonder if I will have a problem in a multi-user environment with
code that changes this variable.

Can someone please review this sample code and let me know if it is an
issue?
Also what is the best way to clean it up? Dim the variable inside each
method?

Thanks!

----------------------------------------------------------------------
Sample code:

Base Class:

'this variable never changes once it is established on app start. Is
this OK?
Public Shared mVar1 As String

'used in sub-classes and is changed inside each method call. Is this a
huge problem?
Public Shared mVar2 As String


Sub-Class:

'method 1:
Public Shared Function Foo() As String
mVar2 = "Some string "
mVar2 &= "some more "
mVar2 &= "and more"
Return mVar2
End Function


'method 2:
Public Shared Function Bar() As String
mVar2 = "Some other string "
mVar2 &= "some more "
mVar2 &= "and more"
Return mVar2
End Function

'methods 3-10 do similar things.
 
S

Scott Allen

Hi Joe:

As you suspect, multiple user's requests might be modifying the
variable at the same time, leading to unexpected results. The easiest
solution is to use a local variable inside of each shared function.
 
J

Joe Fallon

Scott,
Thanks for the bad news. <g>
At least I can recover from this mistake!

I think I will go with:

Public Shared Function Bar() As String
Return "Some other string " &_
"some more " &_
"and more"
End Function
 
G

Guest

Why does the variable have to be shared?
can it not be a Public or Protected Variable/Property?

hth

guy
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top