473,379 Members | 1,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Use of static member variables

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
Nov 18 '05 #1
5 6835
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" <To********@discussions.microsoft.com> wrote in message news:35**********************************@microsof t.com...
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
Nov 18 '05 #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" <To********@discussions.microsoft.com> wrote in message
news:35**********************************@microsof t.com...
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

Nov 18 '05 #3
"Ibrahim Shameeque" <Ib**************@discussions.microsoft.com> wrote in
message news:DF**********************************@microsof t.com...
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
Nov 18 '05 #4
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" <Ib**************@discussions.microsoft.com> wrote in
message news:DF**********************************@microsof t.com...
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

Nov 18 '05 #5
"Ibrahim Shameeque" <Ib**************@discussions.microsoft.com> wrote in
message news:74**********************************@microsof t.com...
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" <Ib**************@discussions.microsoft.com> wrote in message news:DF**********************************@microsof t.com...
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

Nov 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: BCC | last post by:
Hi, I have a class with several member variables that should be initialized according to user input before an object is instantiated. Using a static variable is required here. But, I would...
29
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member...
2
by: katekukku | last post by:
HI, Could anyone please tell me what are static variables and what exactly are there features. I am a little bit confused. Thank You
25
by: Sahil Malik [MVP] | last post by:
So here's a rather simple question. Say in an ASP.NET application, I wish to share common constants as static variables in global.asax (I know there's web.config bla bla .. but lets just say I...
3
by: Colin Desmond | last post by:
I have an MFC Extension DLL that I have flicked the /clr switch on and made a few other tweaks so it compiles and runs in a managed MFC applicaiton. The DLL contains a number of classes which...
1
by: mangalalei | last post by:
A static data member can be of the same class type as that of which it is a member. A nonstatic data member is restricted to being declared as a pointer or a reference to an object of its class. ...
5
by: mast2as | last post by:
Hi guys Here's the class I try to compile (see below). By itself when I have a test.cc file for example that creates an object which is an instance of the class SpectralProfile, it compiles...
6
by: Olumide | last post by:
Hi - I've got a class that contains static member functions alone, all of whose arguments are passed by reference as shown below: class MySpiffyClass{ // no constructor, destructor or...
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
4
by: aaragon | last post by:
Hi everyone, I have a linking error when using gcc4.2 and static member variables. The class template definition is something around the following: template<> class Element<L2_t: public...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.