is a shared variable what I want?

D

Darrel

I'm still a bit confused as to when to use dim vs. shared vs. public, etc.
when declaring a variable.

I have a page that has a few variables I use throughout a variety of
functions within the codebehind. My codebehind looks like this:

-----------------------------------
dim myVariable as string

Private Sub Page_Load()
if not postback, assign a value to myVariable
end sub

Private Sub ButtonClick
do something based on value of myVariable
end function
--------------------------------

I'm setting the value on pageLoad, and what to use the string's value on the
postback.

The only way I've gotten this to work is to declare the myVariable as
shared:

shared myVariable as string

but I, admittedly, am not fully understanding why that is and why somethign
like

public myVariable as string

doesn't work (as it doesn't retain the value on the postback).

-Darrel
 
J

Johann MacDonagh

Hi, it seems you're having a little trouble maintaining a value across page
postbacks. I'll see if I can help.

The main difference between Windows Forms and Web Forms, is that Web Forms
uses HTTP to communicate. Now, by design, HTTP is stateless, meaning that it
has no idea, nor does it care, whether postbacks are from the same request /
user. It relays information from the server to the user, and vice versa.

ASP.net tries to blur the line by building in some virtual state management
into the framework. The fact that you use the IsPostBack property of the
Page shows that it's working. Your page knows whether this is a new request,
or you have already been on the page.

Here's a little visualization for the code you are writing. When the user
first accesses the page, it executes the

dim myVariable as string

line. he Page_Load() method is invoked, and you assign a value to it.
myVariable is a simple member of your Page. When the Page is disposed of
(after it is done rendering and sent to the user), your value is destroyed.

So what can you do? Utilize the same features that the ASP.net team created
to hold the value across page postbacks. ViewState. ViewState is an awesome
feature, and you can see evidence of it on your page (look at the HTML
source, and look at the __ViewState hidden HTML input field (it's
encrypted).

The initial values of all the controls on your page (for example, the Text
property of a Label control), are always going to be there. Every time the
Page loads (for the first visit, as well as postbacks), it loads that
Label's Text property from the HTML source. After that, it looks at the
ViewState to see if changes have been made. If so, then it changes it's
value to the ViewState value. For example, if you have a Label that says
"Hello world!", and then have a button which, when clicked, changes the
Label's Text property to "This is my first program!", that Text property of
the Label is stored in ViewState. The next time your page is posted back
(let's say you have another button that doesn't do anything when clicked),
the Page will load "Hello World!" from the HTML source, and then load "This
is my first program!" from the ViewState (which is carried over because it
is a form field).

So, with that little introduction to ViewState, let's get on to solving your
problem. ViewState is completely open, which means you can store and
retrieve whatever values you desire. Here's a working example of what you
want to achieve.

Private Sub Page_Load()
If Not IsPostBack
ViewState("myValue") = "Whatever Value"
End If
End Sub

Private Sub ButtonClick
Dim myVariable As String

If ViewState("myValue") <> Nothing Then
myVariable = ViewState("myValue")
Else
myVariable = String.Empty
End If

' Do whatever you need to
End Function

So basically, on your Page's first load, it will store a value in the
ViewState. At the end of the Page's life cycle (when it renders all it's
content and sends it to the user), it will also save that value you have in
the __ViewState hidden form field. When the page gets posted back, that
encrypted field gets sent to the web server, which can decrypt it and
restore the value you entered into ViewState("myValue").

To answer your question regarding Public and Shared, you'll need to know a
little about access modifiers. There are actually three when you mention
your situation. Public, Shared, and Private. When you write

Dim myVariable as String

it is, by default, private. That means only the methods inside that code
file can access it's value. If you declare it public, then any code file can
access it. However, these only apply if the Page has been instantiated (for
example, when you create an object:

Dim a As MyClass = New MyClass()

you are creating an instance of that class. The Page is also instantiated.
Somewhere in the framework is something along the lines of:

Dim WhateverYourPageName as Page = new Page()

). Shared is a little different, in that it allows you to access it without
the Page being instantiated. I'm actually quite surprised that declaring the
variable as Shared worked, but, I assume, what you're doing is saving a
GLOBAL value to myVariable. Anyone who uses that page will be using the same
value. I'm sure this isn't something you want.

To conclude, try out my code snippet I posted. If you have any more
problems, feel free to reply!

Happy coding,
Johann MacDonagh
 
D

Darrel

Anyone who uses that page will be using the same value. I'm sure this
isn't something you want.

Aha! Yes. That makes sense...and you're right...I don't want that.

Thanks for that exhaustive explanation. I really appreciate it. I've never
used the viewstate feature. In the past, I was using hidden form elements to
'store' the value on postback, which appears to simply be a more laborous
home-made version of the viewstate string that .net uses automatically.

Again, I really appreciate the explanation. Thanks!

-Darrel
 

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,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top