Use of static member variables

G

Guest

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
 
K

Kevin Spencer

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 said:
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.
 
G

George

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
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
 
J

John Saunders

Ibrahim Shameeque said:
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.
 
G

Guest

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
 
J

John Saunders

Ibrahim Shameeque said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top