473,382 Members | 1,376 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Question about persistence and a global variable

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
Nov 18 '05 #1
5 2342
Hi, Simon,

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

Greetings
Martin
"Simon Harvey" <si**********@the-web-works.co.uk> wrote in message
news:OO*************@TK2MSFTNGP09.phx.gbl...
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

Nov 18 '05 #2
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(){

}
}
Nov 18 '05 #3
Hi, Simon,

"Simon Harvey" <si**********@the-web-works.co.uk> wrote in message
news:#K**************@TK2MSFTNGP09.phx.gbl...
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
public Bob(){

}
}

Nov 18 '05 #4
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.

"Simon Harvey" <si**********@the-web-works.co.uk> wrote in message
news:#K**************@TK2MSFTNGP09.phx.gbl...
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(){

}
}

Nov 18 '05 #5
Simon Harvey wrote:
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).

--
Sebastien Lambla [TheTechnologist]
The Geeky Lazy Bloggy
http://blog.thetechnologist.net
Nov 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Tom Moroow | last post by:
Hi, I'm pretty new to javascript and was wondering how you would piece together a variable name and then assign it a value. I want to create a hidden field and assign it a value based on the value...
8
by: Thomasb | last post by:
With a background in MS SQL Server programming I'm used to temporary tables. Have just started to work with DB2 ver 7 on z/OS and stumbled into the concept of GLOBAL TEMPORARY TABLE. I have...
7
by: Lyn | last post by:
Hi and Season's Greetings to all. I have a question regarding the use of a qualifier word "Global". I cannot find any reference to this in Access help, nor in books or on the Internet. "Global"...
10
by: Simon Harvey | last post by:
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...
7
by: Steve Mauldin | last post by:
I have a public variable that is declared in a public module. This Variable is stored into a Session variable and used to pass data from page to page. I am seeing on my local development box that...
6
by: mb | last post by:
Hi, what would be the best way to store a user id etc..for a multiple user db ... and then be able to access that userid at various points in the app ?? e.g. user signs in with id of...
2
by: Eric Cathell | last post by:
I am using dOOdads as my persistence layer. I have a business object Person that sits in a Project called DOMAIN I have 2 dOOdads in a project called BLL. one is _Person and is declared...
6
by: mirandacascade | last post by:
Assume the following: 1) multi-user environment 2) when user opens app, want to run some code that retrieves some information specific to the user...retrieving this information is somewhat i/o...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.