469,329 Members | 1,418 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,329 developers. It's quick & easy.

variable scope in a webform only

Max
Please somebody can shed a light...
How can I have a variable visible and modifiable, inside one and only
webform?
I mean , I d like to see that variable from all the Sub of that webform
code, but if I window.open another instance of that webform, that variable
should be independant even if the name is the same.

I've declared that var as Public shared, but I realize now that that
variable is modifiable from all the webforms, and even from another user
session! Is this possible?
Should I use viewstate, or there is a right declaration that I should use
and that I don't know?
Thanks Max

Mar 9 '07 #1
7 1630
Just declare the variable inside the page class, but not inside of any
particular method and you'll be able to call the variable from anywhere
inside the page class. If another browser window were to be opened, it
would be a different instance of the class and so one variable's value
wouldn't affect the other.
"Max" <ma******************@fastwebnet.itwrote in message
news:cn****************@tornado.fastwebnet.it...
Please somebody can shed a light...
How can I have a variable visible and modifiable, inside one and only
webform?
I mean , I d like to see that variable from all the Sub of that webform
code, but if I window.open another instance of that webform, that variable
should be independant even if the name is the same.

I've declared that var as Public shared, but I realize now that that
variable is modifiable from all the webforms, and even from another user
session! Is this possible?
Should I use viewstate, or there is a right declaration that I should use
and that I don't know?
Thanks Max



Mar 9 '07 #2
"Max" <ma******************@fastwebnet.itwrote in message
news:cn****************@tornado.fastwebnet.it...
Please somebody can shed a light...
How can I have a variable visible and modifiable, inside one and only
webform?
I mean , I d like to see that variable from all the Sub of that webform
code, but if I window.open another instance of that webform, that variable
should be independant even if the name is the same.

I've declared that var as Public shared
Don't use Shared. Just Public. Shared means "shared across all instances of
this class".

John
Mar 10 '07 #3
Max
Hi Scott
I tried that, but it seems that the variable cannot be seen by all the SUBs
of that page.
If for example, Button1_click sets abc="hello" , Button2_click cannot see
abc value.
How do you declare abc inside the page class?
Thanks!
Max
"Scott M." <s-***@nospam.nospamha scritto nel messaggio
news:ed**************@TK2MSFTNGP06.phx.gbl...
Just declare the variable inside the page class, but not inside of any
particular method and you'll be able to call the variable from anywhere
inside the page class. If another browser window were to be opened, it
would be a different instance of the class and so one variable's value
wouldn't affect the other.
"Max" <ma******************@fastwebnet.itwrote in message
news:cn****************@tornado.fastwebnet.it...
>Please somebody can shed a light...
How can I have a variable visible and modifiable, inside one and only
webform?
I mean , I d like to see that variable from all the Sub of that webform
code, but if I window.open another instance of that webform, that
variable should be independant even if the name is the same.

I've declared that var as Public shared, but I realize now that that
variable is modifiable from all the webforms, and even from another user
session! Is this possible?
Should I use viewstate, or there is a right declaration that I should use
and that I don't know?
Thanks Max




Mar 10 '07 #4
That's because Page class is instantiated for every request so classes
declared on page class body, are in the same instance only during a single
request. On next request (subsequent postback is such also) it is a new
instance and therefore new set of variables. If you want something that
survives for subsequent postbacks use ViewState.

As was said, do *not* use static(Shared) variables on Page class. They bring
more harm than good.
--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net
"Max" <ma******************@fastwebnet.itwrote in message
news:UW****************@tornado.fastwebnet.it...
Hi Scott
I tried that, but it seems that the variable cannot be seen by all the
SUBs of that page.
If for example, Button1_click sets abc="hello" , Button2_click cannot see
abc value.
How do you declare abc inside the page class?
Thanks!
Max
"Scott M." <s-***@nospam.nospamha scritto nel messaggio
news:ed**************@TK2MSFTNGP06.phx.gbl...
>Just declare the variable inside the page class, but not inside of any
particular method and you'll be able to call the variable from anywhere
inside the page class. If another browser window were to be opened, it
would be a different instance of the class and so one variable's value
wouldn't affect the other.
"Max" <ma******************@fastwebnet.itwrote in message
news:cn****************@tornado.fastwebnet.it.. .
>>Please somebody can shed a light...
How can I have a variable visible and modifiable, inside one and only
webform?
I mean , I d like to see that variable from all the Sub of that webform
code, but if I window.open another instance of that webform, that
variable should be independant even if the name is the same.

I've declared that var as Public shared, but I realize now that that
variable is modifiable from all the webforms, and even from another user
session! Is this possible?
Should I use viewstate, or there is a right declaration that I should
use and that I don't know?
Thanks Max




Mar 10 '07 #5
"Teemu Keiski" <jo****@aspalliance.comwrote in message
news:u1**************@TK2MSFTNGP06.phx.gbl...
As was said, do *not* use static(Shared) variables on Page class. They
bring more harm than good.
Yes indeed.

Straight out of on-line help: "While an instance of a class contains a
separate copy of all instance fields of the class, there is only one copy of
each static field."

That might be fine for entities which *are* unique across all instances of
the class e.g. maybe the database connection string or mail server name, but
for entities which are user-specific or session-specific, static (Shared)
variables must be avoided at all costs...
Mar 10 '07 #6
He only wants it accessible from one page request at a time (as he stated in
the OP).

"Teemu Keiski" <jo****@aspalliance.comwrote in message
news:u1**************@TK2MSFTNGP06.phx.gbl...
That's because Page class is instantiated for every request so classes
declared on page class body, are in the same instance only during a single
request. On next request (subsequent postback is such also) it is a new
instance and therefore new set of variables. If you want something that
survives for subsequent postbacks use ViewState.

As was said, do *not* use static(Shared) variables on Page class. They
bring more harm than good.
--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net
"Max" <ma******************@fastwebnet.itwrote in message
news:UW****************@tornado.fastwebnet.it...
>Hi Scott
I tried that, but it seems that the variable cannot be seen by all the
SUBs of that page.
If for example, Button1_click sets abc="hello" , Button2_click cannot see
abc value.
How do you declare abc inside the page class?
Thanks!
Max
"Scott M." <s-***@nospam.nospamha scritto nel messaggio
news:ed**************@TK2MSFTNGP06.phx.gbl...
>>Just declare the variable inside the page class, but not inside of any
particular method and you'll be able to call the variable from anywhere
inside the page class. If another browser window were to be opened, it
would be a different instance of the class and so one variable's value
wouldn't affect the other.
"Max" <ma******************@fastwebnet.itwrote in message
news:cn****************@tornado.fastwebnet.it. ..
Please somebody can shed a light...
How can I have a variable visible and modifiable, inside one and only
webform?
I mean , I d like to see that variable from all the Sub of that webform
code, but if I window.open another instance of that webform, that
variable should be independant even if the name is the same.

I've declared that var as Public shared, but I realize now that that
variable is modifiable from all the webforms, and even from another
user session! Is this possible?
Should I use viewstate, or there is a right declaration that I should
use and that I don't know?
Thanks Max




Mar 10 '07 #7
You're not doing it right then. Take a look at this simple class....

Public Class Test

Dim x As Integer

Public Sub foo1()
x = 7
End Sub

Public Sub foo2()
txtSomthing.Text = x
End Sub

End Class

As you can see, the variable "x" is declared in the class but not in any
particular sub. This variable is now accessible anywhere else in this
class, and only in this instance of the class.

"Max" <ma******************@fastwebnet.itwrote in message
news:UW****************@tornado.fastwebnet.it...
Hi Scott
I tried that, but it seems that the variable cannot be seen by all the
SUBs of that page.
If for example, Button1_click sets abc="hello" , Button2_click cannot see
abc value.
How do you declare abc inside the page class?
Thanks!
Max
"Scott M." <s-***@nospam.nospamha scritto nel messaggio
news:ed**************@TK2MSFTNGP06.phx.gbl...
>Just declare the variable inside the page class, but not inside of any
particular method and you'll be able to call the variable from anywhere
inside the page class. If another browser window were to be opened, it
would be a different instance of the class and so one variable's value
wouldn't affect the other.
"Max" <ma******************@fastwebnet.itwrote in message
news:cn****************@tornado.fastwebnet.it.. .
>>Please somebody can shed a light...
How can I have a variable visible and modifiable, inside one and only
webform?
I mean , I d like to see that variable from all the Sub of that webform
code, but if I window.open another instance of that webform, that
variable should be independant even if the name is the same.

I've declared that var as Public shared, but I realize now that that
variable is modifiable from all the webforms, and even from another user
session! Is this possible?
Should I use viewstate, or there is a right declaration that I should
use and that I don't know?
Thanks Max





Mar 10 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Grant Wagner | last post: by
134 posts views Thread by James A. Donald | last post: by
4 posts views Thread by Gery D. Dorazio | last post: by
23 posts views Thread by Russ Chinoy | last post: by
pbmods
1 post views Thread by pbmods | last post: by
112 posts views Thread by istillshine | last post: by
3 posts views Thread by SRoubtsov | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.