Shared Public Variables and Shared Methods

Discussion in 'ASP .Net' started by Joe Fallon, Jul 15, 2004.

  1. Joe Fallon

    Joe Fallon Guest

    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
     
    Joe Fallon, Jul 15, 2004
    #1
    1. Advertising

  2. Joe Fallon

    Scott Allen Guest

    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"
    <> 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.
     
    Scott Allen, Jul 15, 2004
    #2
    1. Advertising

  3. Joe Fallon

    Joe Fallon Guest

    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:...
    > 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"
    > <> 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, Jul 15, 2004
    #3
  4. 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
    >
    >
    >
    >
    >
     
    =?Utf-8?B?Z3V5?=, Jul 16, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman
    Replies:
    1
    Views:
    1,362
    smith
    Dec 8, 2004
  2. SpamProof
    Replies:
    0
    Views:
    576
    SpamProof
    Oct 21, 2003
  3. avnrao
    Replies:
    0
    Views:
    3,366
    avnrao
    May 7, 2004
  4. Steve
    Replies:
    1
    Views:
    253
  5. Robert
    Replies:
    16
    Views:
    193
Loading...

Share This Page