Hi Dega,
Although I was hoping to avoid them, you inspired me and I have been
experimenting with classes and went thru some walkthoroughs.
Good for you! You'll be glad you did. The hardest step is the first.
I can create
them and "get & set" info into and from them. However can only do it in
one
VB procedure.
I'm not sure what you mean by this.
My program consists of several Web pages . What I really want to do is
have
information that I get earlier in various Web pages that are being
processed with VB , available several pages later, where I need the pieces
of
info to do final processing.
Should I be using session variables or can I use a class to save
information earlier during the processing of some pages and retrieve it
later? If so how do I get it later when I need it? Can I declare a new
class
instance so that iitis available from any page processing?
Okay, we have a couple of issues here to clarify. First, I didn't know that
you're working with an ASP.Net application. This is a complicated
application to work on, because ASP.Net is about a client-server app hosted
on an HTTP web server. HTTP is stateless, and is a Request/Response
protocol. Let me explain a bit first:
A web client (such as a browser) makes an HTTP request for a "resource" to a
web server. The server responds by finding the resource and returning it to
the client. The server remembers nothing after that. The next request from
the same client is therefore a brand new request. The server doesn't
remember the previous one.
ASP.Net is a framework designed to act as an application that is hosted by a
web server. Unlike the web server, it does have persistent memory, but is
designed to work in the HTTP environment. It has an application memory,
which is not client-specific, but is available to any class instance in the
application. It also has Session memory, a Collection it maintains where it
can remember some data about each client that has recently sent a Request.
Because of the stateless nature of HTTP, there is no way for a client to
"disconnect" from the application, no way for the application to know which
Request will be the last from any client. Therefore, in order to prevent
memory from building up until ti runs out, the Session times out 20 minutes
after the last Request from any client. Remember, this is a client-server
app, which may have many clients, each of which will require some server
resources. Therefore, keeping memory limited is the only way to go.
Again, because of the HTTP environment, and the fact that any given page is
not likely to be requested repeatedly, that is, the client will request
various pages at will, the Page class created when a Request comes in is not
persisted in memory. The class is instantiated when the Request is received,
lives for the duration of the processing of the Request, and then destroyed
when the Response is sent.
How a Page retains its state is managed by a rather complex framework of
tricks. The most important of these is ViewState. An HTML Form can post data
to the server with its Request. This is why an ASP.Net Page class is always
hosted in a WebForm. When the page is sent to the client, it contains both
JavaScript and hidden form fields that are used on the client to prepare
information about the state of the HTML document on the client which can be
posted back to the server. This way, the page's Form Post can "remind" the
server about what state the HTML document on the client was in when it
posted back. Then, the server-side application re-creates the Page class,
and restores it to the current client state for processing.
What this means is that, while it may seem like it is the same Page class
instance on the server with each PostBack, it is a new instance every time.
In essence, each instance is a "restoration" of the previous instance. That
is why the class doesn't seem to persist its memory state.
Static (Shared) classes and class members are different, in that there is
only 1 instance in the Application, and because of this, it can remain in
Application memory for the lifetime of the application. However, as there is
only 1 instance, it is used ("shared") by all clients. Anything one client
does to it is seen by any other client which accesses it. As you can see,
this is only useful if the data is not client-specific.
So, you have several alternative ways of persisting and restoring
client-specific data in your Page class, and those alternatives are the same
ones that ASP.Net has for its application. That is, you can use Page
ViewState, Session state, or a database. These are the most common ways.
Whenever possible, avoid Session state, as again, this requires duplicate
memory on the server for each client. If I give you a dollar, you have only
received 1 dollar. But if a million people each give you a dollar, you have
received 1 million dollars.
--
HTH,
Kevin Spencer
Microsoft MVP
Professional Numbskull
Show me your certification without works,
and I'll show my certification
*by* my works.