Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Use of static member variables

Reply
Thread Tools

Use of static member variables

 
 
=?Utf-8?B?VG9tIFBlYXJzb24=?=
Guest
Posts: n/a
 
      07-16-2004
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
 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      07-16-2004
> 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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
 
 
 
George
Guest
Posts: n/a
 
      07-16-2004
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" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
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
 
Reply With Quote
 
John Saunders
Guest
Posts: n/a
 
      07-16-2004
"Ibrahim Shameeque" <(E-Mail Removed)> wrote in
message news(E-Mail Removed)...
> 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


 
Reply With Quote
 
=?Utf-8?B?SWJyYWhpbSBTaGFtZWVxdWU=?=
Guest
Posts: n/a
 
      07-19-2004
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" <(E-Mail Removed)> wrote in
> message news(E-Mail Removed)...
> > 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
>
>
>

 
Reply With Quote
 
John Saunders
Guest
Posts: n/a
 
      07-19-2004
"Ibrahim Shameeque" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote

in
> > message news(E-Mail Removed)...
> > > 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
> >
> >
> >



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM
performance of static member function vs. instance member function 0to60 C++ 4 11-21-2003 05:25 PM



Advertisments