Use of static member variables

Discussion in 'ASP .Net' started by =?Utf-8?B?VG9tIFBlYXJzb24=?=, Jul 16, 2004.

  1. What is the scope of static variable when programming in ASP.NET?

    For example I have a control class that uses static callbacks so that another window can pass a list of items to it. The control doesn't have access to this other object so needs to subscribe to the static callback. As such the static callback then needs to get hold of the real control instance which it can do via a member vaiarble which is set to the instance of the control on creation. This all works fine.

    But comming from a non web background I am unsure of the scrope of these static variables. For example if the page containng this control is servered to multiple users at the same time is this static vairiable going to be used for all sessions, or is the static variable session safe?

    I really need an answer to this so if the info I have provided is insufficient please let me know what further info you require.

    Cheers
    Tom
     
    =?Utf-8?B?VG9tIFBlYXJzb24=?=, Jul 16, 2004
    #1
    1. Advertising

  2. > But comming from a non web background I am unsure of the scrope of these
    static variables.

    Static variables in ASP.Net are the same as static variables elsewhere.
    Static. Stored in the heap.

    --
    HTH,
    Kevin Spencer
    ..Net Developer
    Microsoft MVP
    Big things are made up
    of lots of little things.

    "Tom Pearson" <> wrote in message
    news:...
    > What is the scope of static variable when programming in ASP.NET?
    >
    > For example I have a control class that uses static callbacks so that

    another window can pass a list of items to it. The control doesn't have
    access to this other object so needs to subscribe to the static callback. As
    such the static callback then needs to get hold of the real control instance
    which it can do via a member vaiarble which is set to the instance of the
    control on creation. This all works fine.
    >
    > But comming from a non web background I am unsure of the scrope of these

    static variables. For example if the page containng this control is servered
    to multiple users at the same time is this static vairiable going to be used
    for all sessions, or is the static variable session safe?
    >
    > I really need an answer to this so if the info I have provided is

    insufficient please let me know what further info you require.
    >
    > Cheers
    > Tom
     
    Kevin Spencer, Jul 16, 2004
    #2
    1. Advertising

  3. =?Utf-8?B?VG9tIFBlYXJzb24=?=

    George Guest

    All instances of the class share the static variables.

    So if page is served to multiple users the same static variables are used for different sessions.
    Also they do not die when no pages is served to the browsers.

    Think of them as a global variables. Until that process is unloaded from memory they will exist and keep the values.

    The drawback. You need to synchronize access to them from different threads (different browser requests).
    If you are doing read-only operations then probably you do not need to synchronize access.

    For example i keep all config settings (like DB connection string ) in static variables of the Global class.

    George
    My Site - Body Jewelry
    "Tom Pearson" <> wrote in message news:...
    What is the scope of static variable when programming in ASP.NET?

    For example I have a control class that uses static callbacks so that another window can pass a list of items to it. The control doesn't have access to this other object so needs to subscribe to the static callback. As such the static callback then needs to get hold of the real control instance which it can do via a member vaiarble which is set to the instance of the control on creation. This all works fine.

    But comming from a non web background I am unsure of the scrope of these static variables. For example if the page containng this control is servered to multiple users at the same time is this static vairiable going to be used for all sessions, or is the static variable session safe?

    I really need an answer to this so if the info I have provided is insufficient please let me know what further info you require.

    Cheers
    Tom
     
    George, Jul 16, 2004
    #3
  4. "Ibrahim Shameeque" <> wrote in
    message news:D...
    > Hi
    > speaking about static variables, if you have an application-state

    variable, a public variable in a module and a static variable ( which can be
    declared only in procedure level) and then write something like below in
    webform1.aspx
    >
    > 'Create a textbox and a button on page
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As

    System.EventArgs) Handles MyBase.Load
    > 'Put user code to initialize the page here
    > Static x As Integer
    > If TextBox1.Text <> "" Then
    > x = TextBox1.Text
    > Application("gettime") = TextBox1.Text
    > stest = TextBox1.Text 'sTest declared in module1.vb
    > End If
    >
    > Response.Write("stat:" & x & "<br>")
    > Response.Write("App:" & Application("gettime") & "<br>")
    > Response.Write("public:" & sTest & "<br>")
    > End Sub
    >
    > Build the project , open two instances of your browser , change the value

    of text box in one instance and submit the page . In another instance of the
    browser, just sumbit the page without any value in the textbox, you find
    only the application-state variable and the public variable retains the
    value and static variable looses scope. Doesn't this mean that static
    variable are session specific as Tom says?

    No. Statics are application-wide. Put that static "x" above the "Private
    Sub" and you'll see what's meant.
    --
    John Saunders
    johnwsaundersiii at hotmail
     
    John Saunders, Jul 16, 2004
    #4
  5. Then why am i getting an error if i declare static above the procedure. it says 'Static is not valid on a member variable declaration'. It is just me?. Thanks
    --
    Ibrahim



    "John Saunders" wrote:

    > "Ibrahim Shameeque" <> wrote in
    > message news:D...
    > > Hi
    > > speaking about static variables, if you have an application-state

    > variable, a public variable in a module and a static variable ( which can be
    > declared only in procedure level) and then write something like below in
    > webform1.aspx
    > >
    > > 'Create a textbox and a button on page
    > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As

    > System.EventArgs) Handles MyBase.Load
    > > 'Put user code to initialize the page here
    > > Static x As Integer
    > > If TextBox1.Text <> "" Then
    > > x = TextBox1.Text
    > > Application("gettime") = TextBox1.Text
    > > stest = TextBox1.Text 'sTest declared in module1.vb
    > > End If
    > >
    > > Response.Write("stat:" & x & "<br>")
    > > Response.Write("App:" & Application("gettime") & "<br>")
    > > Response.Write("public:" & sTest & "<br>")
    > > End Sub
    > >
    > > Build the project , open two instances of your browser , change the value

    > of text box in one instance and submit the page . In another instance of the
    > browser, just sumbit the page without any value in the textbox, you find
    > only the application-state variable and the public variable retains the
    > value and static variable looses scope. Doesn't this mean that static
    > variable are session specific as Tom says?
    >
    > No. Statics are application-wide. Put that static "x" above the "Private
    > Sub" and you'll see what's meant.
    > --
    > John Saunders
    > johnwsaundersiii at hotmail
    >
    >
    >
     
    =?Utf-8?B?SWJyYWhpbSBTaGFtZWVxdWU=?=, Jul 19, 2004
    #5
  6. "Ibrahim Shameeque" <> wrote in
    message news:...
    > Then why am i getting an error if i declare static above the procedure. it

    says 'Static is not valid on a member variable declaration'. It is just me?.
    Thanks

    No, not just you. I screwed up. I didn't realize that VB.NET had the concept
    of "Static" as different from "Shared". I do not know if a "Static" variable
    will receive interference from other requests, but I suspect that it will.
    The VB.NET language doesn't know anything about requests, or threads, so if
    a Static variable keeps its value across multiple function calls, then I
    suspect that all threads or requests calling that function will see the same
    value, and I suspect that one thread could interfere with another.
    --
    John Saunders
    johnwsaundersiii at hotmail



    > "John Saunders" wrote:
    >
    > > "Ibrahim Shameeque" <> wrote

    in
    > > message news:D...
    > > > Hi
    > > > speaking about static variables, if you have an application-state

    > > variable, a public variable in a module and a static variable ( which

    can be
    > > declared only in procedure level) and then write something like below

    in
    > > webform1.aspx
    > > >
    > > > 'Create a textbox and a button on page
    > > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As

    > > System.EventArgs) Handles MyBase.Load
    > > > 'Put user code to initialize the page here
    > > > Static x As Integer
    > > > If TextBox1.Text <> "" Then
    > > > x = TextBox1.Text
    > > > Application("gettime") = TextBox1.Text
    > > > stest = TextBox1.Text 'sTest declared in module1.vb
    > > > End If
    > > >
    > > > Response.Write("stat:" & x & "<br>")
    > > > Response.Write("App:" & Application("gettime") & "<br>")
    > > > Response.Write("public:" & sTest & "<br>")
    > > > End Sub
    > > >
    > > > Build the project , open two instances of your browser , change the

    value
    > > of text box in one instance and submit the page . In another instance of

    the
    > > browser, just sumbit the page without any value in the textbox, you find
    > > only the application-state variable and the public variable retains the
    > > value and static variable looses scope. Doesn't this mean that static
    > > variable are session specific as Tom says?
    > >
    > > No. Statics are application-wide. Put that static "x" above the "Private
    > > Sub" and you'll see what's meant.
    > > --
    > > John Saunders
    > > johnwsaundersiii at hotmail
    > >
    > >
    > >
     
    John Saunders, Jul 19, 2004
    #6
    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. 0to60
    Replies:
    4
    Views:
    518
    jeffc
    Nov 21, 2003
  2. Siemel Naran
    Replies:
    4
    Views:
    809
    Micah Cowan
    Jan 12, 2005
  3. aling
    Replies:
    6
    Views:
    484
    Xiaobin.Huang
    Oct 30, 2005
  4. dolphin
    Replies:
    3
    Views:
    1,349
    Pete Becker
    Dec 5, 2007
  5. Replies:
    9
    Views:
    959
Loading...

Share This Page