variable scope in a webform only

M

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
 
S

Scott M.

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

John Saunders

Max said:
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
 
M

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
 
T

Teemu Keiski

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.
 
M

Mark Rae

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...
 
S

Scott M.

He only wants it accessible from one page request at a time (as he stated in
the OP).
 
S

Scott M.

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.
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top