"Page" class and utility class

G

Guest

Hello All,

I have written a webform which is by default derived from "Page" class. I
have coded another utility class with few methods and an object of this class
is instantiated from the webfom class.

Methods inside the utility class set few global variables which act as
mediums of persistence to hold data. Now my question is all works fine during
my initial request and all the global variables are set correctly, but during
later requests I doubt that global variables are holding null references. Can
anyone give me a suitable explanation on this?

I know that Page class is completely destroyed in the "Unload" method of the
lifecycle but how about the utility class? Would it even be destroyed along
with the webform class?

Thanks for your pointers!!
 
C

Curt_C [MVP]

You can persist through app variables, to some extent. Beyond that use a
DB/File/etc to persist data.
 
G

Guest

Does your reply mean that the utility class's object is also destroyed in the
Page's Unload method?
 
B

bruce barker

it depend on how the utility class is coded and used. if you webform creates
an instance of the utility class, it will be released at the end of the page
process. all pages will see there own instance data. if the utiltity class
has any shared or static variables, these live the life of the appdomain,
and any page request sees the same values. if you do not use some locking
techinque you will have threading issues.

-- bruce (sqlwork.com)

| Hello All,
|
| I have written a webform which is by default derived from "Page" class. I
| have coded another utility class with few methods and an object of this
class
| is instantiated from the webfom class.
|
| Methods inside the utility class set few global variables which act as
| mediums of persistence to hold data. Now my question is all works fine
during
| my initial request and all the global variables are set correctly, but
during
| later requests I doubt that global variables are holding null references.
Can
| anyone give me a suitable explanation on this?
|
| I know that Page class is completely destroyed in the "Unload" method of
the
| lifecycle but how about the utility class? Would it even be destroyed
along
| with the webform class?
|
| Thanks for your pointers!!
 
G

Guest

Diffident said:
I have written a webform which is by default derived from "Page" class. I
have coded another utility class with few methods and an object of this class
is instantiated from the webfom class.

Methods inside the utility class set few global variables which act as
mediums of persistence to hold data. (abridged)

I know that Page class is completely destroyed in the "Unload" method of the
lifecycle but how about the utility class? Would it even be destroyed along
with the webform class?
You haven't got "global variables" in object oriented programming
environments, so I except that you refer to either:
1. Field on the utility class
2. Application or Session variables.

If the utility class is instantiated from the webform class and no other
"living" class holds a reference to the class the utility class is
marked for garbage collection. A new instance of the utility class will
be created when a new request for the page is handled.
If you store values data in the fields of the utility class (scenario 1)
the data dies with the class.
If you store the data in either session or application variables
(scenario 2), the data can be retrieved on subsequent requests.

A third posibility is to store the data in the page's view state.
To store a value in and retrieve it from the view state do this (C#):
if (Page.IsPostBack) {
// Read from view state
RegTot=(string) ViewState["MyValue"];
} else {
// Store in view state
ViewState["MyValue"] = RegTot;
}

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top