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

Shared Public Variables and Shared Methods

P: n/a
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.
--
Joe Fallon


Nov 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

--
Scott
http://www.OdeToCode.com
On Thu, 15 Jul 2004 12:22:04 -0400, "Joe Fallon"
<jf******@nospamtwcny.rr.com> wrote:
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.


Nov 18 '05 #2

P: n/a
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

--
Joe Fallon
"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:l7********************************@4ax.com...
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.

--
Scott
http://www.OdeToCode.com
On Thu, 15 Jul 2004 12:22:04 -0400, "Joe Fallon"
<jf******@nospamtwcny.rr.com> wrote:
I have a Shared varibale in a base class and all the Shared methods in thesub-classes use it (and CHANGE it).
I thought I was saving myself the "trouble" of Dimming this variable insideeach Shared method.
But now I wonder if I will have a problem in a multi-user environment withcode 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 ahuge 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.

Nov 18 '05 #3

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

hth

guy

"Joe Fallon" wrote:
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.
--
Joe Fallon


Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.