Static is REALLY Static!

Discussion in 'ASP .Net' started by Paul W, May 3, 2005.

  1. Paul W

    Paul W Guest

    In a standalone module (VB) behind my asp.net application, I have a couple
    of utility routines that do a simple (fixed) data-retrieval (eg. "select
    permissions from tblusers where userid=" & thisuser.

    I figured I'd be smart and save multiple db calls for the same page request
    by doing:

    Function GetPerms() as string
    Static myperms as string
    if myperms="" then
    {get value from db}
    myperms=valueFromDb 'store the retrieved data in a Static variable for
    use on the next request
    else
    GetPerms=myperms
    endif
    End Function

    Well, blow me down, I find that the static values persist across different
    page requests - even across multiple browser-sessions!!!

    Can someone please enlighten me as to what's goin on here?

    Thanks,
    Paul.
     
    Paul W, May 3, 2005
    #1
    1. Advertising

  2. Paul W

    Karl Seguin Guest

    There isn't too much enlightening to do...that's what static is.

    It isn't tied to a page request, or a specific user...instead it's tied to
    the application domain...You can read more from:
    http://www.panopticoncentral.net/archive/2004/04/19/964.aspx

    Without knowing much more about what ur doing, I think you'd typically you'd
    use something like the HttpCache or Session for what you are trying to
    do...

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
    http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
    come!)


    "Paul W" <> wrote in message
    news:uo$...
    > In a standalone module (VB) behind my asp.net application, I have a couple
    > of utility routines that do a simple (fixed) data-retrieval (eg. "select
    > permissions from tblusers where userid=" & thisuser.
    >
    > I figured I'd be smart and save multiple db calls for the same page
    > request by doing:
    >
    > Function GetPerms() as string
    > Static myperms as string
    > if myperms="" then
    > {get value from db}
    > myperms=valueFromDb 'store the retrieved data in a Static variable for
    > use on the next request
    > else
    > GetPerms=myperms
    > endif
    > End Function
    >
    > Well, blow me down, I find that the static values persist across different
    > page requests - even across multiple browser-sessions!!!
    >
    > Can someone please enlighten me as to what's goin on here?
    >
    > Thanks,
    > Paul.
    >
    >
    >
     
    Karl Seguin, May 3, 2005
    #2
    1. Advertising

  3. Paul W

    Guest

    Hi,


    for some reason static variables in asp.net have application-wide scope.
    It's almost the same as Application("myperms"). You could rewrite your
    code:


    Private myperms as string
    Function GetPerms() as string
    if myperms="" then
    {get value from db}
    myperms=valueFromDb 'store the retrieved data in a Static variable for
    use on the next request
    else
    GetPerms=myperms
    endif
    End Function




    Dr. Jones






    On Mon, 2 May 2005 20:54:37 -0400, Paul W <> wrote:

    > In a standalone module (VB) behind my asp.net application, I have a
    > couple
    > of utility routines that do a simple (fixed) data-retrieval (eg. "select
    > permissions from tblusers where userid=" & thisuser.
    >
    > I figured I'd be smart and save multiple db calls for the same page
    > request
    > by doing:
    >
    > Function GetPerms() as string
    > Static myperms as string
    > if myperms="" then
    > {get value from db}
    > myperms=valueFromDb 'store the retrieved data in a Static variable
    > for
    > use on the next request
    > else
    > GetPerms=myperms
    > endif
    > End Function
    >
    > Well, blow me down, I find that the static values persist across
    > different
    > page requests - even across multiple browser-sessions!!!
    >
    > Can someone please enlighten me as to what's goin on here?
    >
    > Thanks,
    > Paul.
    >
    >




    --
    Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
     
    , May 3, 2005
    #3
    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. Amir
    Replies:
    3
    Views:
    621
  2. nc
    Replies:
    1
    Views:
    528
    nice.guy.nige
    Feb 3, 2005
  3. Replies:
    2
    Views:
    367
  4. Jeannie
    Replies:
    15
    Views:
    916
    Jeannie
    Aug 30, 2005
  5. =?Utf-8?B?TFc=?=
    Replies:
    8
    Views:
    444
    =?Utf-8?B?TFc=?=
    Apr 25, 2007
Loading...

Share This Page