Question about persistence and a global variable

S

Simon Harvey

Hi everyone,

Can anyone tell me if I declare a global variable in my pages code behind,
is it persisted if the page does a post back, or do I need to add the object
to the session object in order to persist it.

Is the session the best mechnism for persisiting this object

Thanks everyone

Simon
 
M

Martin Dechev

Hi, Simon,

Please, define "global variable". Post some short example and maybe someone
will be able to answer your question.

Greetings
Martin
 
S

Simon Harvey

Hi,

By Global Variable I mean a variable that has been declared inside a class
but outside any methods.

eg

public class BoB{
private string name = Bob; // Variable is global

public Bob(){

}
}
 
M

Martin Dechev

Hi, Simon,

Simon Harvey said:
Hi,

By Global Variable I mean a variable that has been declared inside a class
but outside any methods.

eg

public class BoB{
private string name = Bob; // Variable is global

It is called field of the class and, no, it will not persist its value
between the posts because on each request for the page (posted or not) a new
instance of the class is instantiated. If you want to keep something in the
page between postbacks of a single user only - use the ViewState and wrap
the value in a property:

public string Foo
{
get
{
if(ViewState["Foo"] != null)
return ViewState["Foo"] as string;
return "";// or some default value, or null
}
set{ViewState["Foo"] = value;}
}

If you want to persist a value per user use a Session item or Cookie.

If you want to persist a value for the lifetime of the application - use an
Application item. Alternatively (but requires a bit more specific desing),
you can persist data in the Cache of the HttpContext.

If you want to persist a value for the lifetime of the AppDomain (until your
assembly gets unloaded from the memory) create a static field in some class.
You can set the appropriate accessibility modifier in this case - private,
public etc.

I might have missed something but these are the most used ways.

Hope this helps
Martin
 
K

Kevin Spencer

Hi Simon,

There are 2 separate components to an ASP.Net page: The server-side class,
which does the work and returns the client-side document to the browser. The
server-side class exists for milliseconds. Any properties or field values
(what you refer to as a "global variable" is called a "field" in the class)
that are set by default will remain (be initialized to their default
values). Any properties or field values that were changed dynamically during
the last Request are re-initialized to their default values. Remember that
the server-side class has no knowledge of any instance that existed prior to
its current instantiation. This is why persistence mediums such as
ViewState, Session, and Application Cache exist.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
S

Sebastien Lambla

Simon said:
Hi everyone,

Can anyone tell me if I declare a global variable in my pages code behind,
is it persisted if the page does a post back, or do I need to add the object
to the session object in order to persist it.

Is the session the best mechnism for persisiting this object

Hi Simon!

The asp.net model doesn't automatically persist anything, and each time
the page is called (be it the first time or on postback) you have to
recreate everything.

The Session object is where you can get persistence on a per user basis.
Be aware that as the user count increase, so does the memory requirement
for using session objects. You might also want to think about how the
Session persistance is used in a web farm environment, because it will
mostly mean running the session from an external process or from a
remote sql database, which if used excensively can become a throughput
bottelneck.

You can also use the ViewState. In that case the data you persist is
serialized inside the web page, and the server doesn't pay the cost of
the persistence, and the web farm configuration is nearly automatic.
OTHA, the web page becomes bigger, the performance can decrease by the
deserialization cost, and the data transfer increases (which also
increase the responsiveness of the application).

All this to say that there's not one way of doing persistence, you need
to evaluate your needs. Try both solutions, and others if you want
(external sql you manage yourself, etc), and do some performance testing
to know where is the right thing. You might want to abstract this in a
Persistence class that would easily let you switch between different
solutions (mainly, using ViewState or Session and change that on the fly).
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top