Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Shared Public Variables and Shared Methods

Reply
Thread Tools

Shared Public Variables and Shared Methods

 
 
Joe Fallon
Guest
Posts: n/a
 
      07-15-2004
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




 
Reply With Quote
 
 
 
 
Scott Allen
Guest
Posts: n/a
 
      07-15-2004
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"
<(E-Mail Removed)> 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.


 
Reply With Quote
 
 
 
 
Joe Fallon
Guest
Posts: n/a
 
      07-15-2004
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:(E-Mail Removed)...
> 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"
> <(E-Mail Removed)> 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.

>



 
Reply With Quote
 
=?Utf-8?B?Z3V5?=
Guest
Posts: n/a
 
      07-16-2004
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
>
>
>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola MCSE 4 11-15-2006 02:40 AM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola Microsoft Certification 3 11-14-2006 05:18 PM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola MCSD 3 11-14-2006 05:18 PM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd realexxams@yahoo.com Microsoft Certification 0 05-10-2006 02:35 PM
microsoft.public.dotnet.faqs,microsoft.public.dotnet.framework,microsoft.public.dotnet.framework.windowsforms,microsoft.public.dotnet.general,microsoft.public.dotnet.languages.vb Charles A. Lackman ASP .Net 1 12-08-2004 07:08 PM



Advertisments