class instance

G

Guest

Hi,

i've written a class which does some calculations for my web application.
These informatinos are different for each page request - the current user is
not important.

i have about 10 aspx pages and 20 ascx user controls. In most of these
pages/user controls i need the informations of this class.

First i created an instance of this class in every page/user control i
needed the information with the result that a page with 10 usercontrols
calculated the same information 11 times until the request is completed
(=many redundant/identical calculations).

So i created a static public instance of this class in the global.asax
Application_PreRequestHandlerExecute event. The class is now created just
once per page request and i can access it from the page and usercontrols like
Global.MyClass.MyPoperties.

But i'm not happy with this because i'm getting "accidentally" errors like
"Object has been disposed" when i access properties of this class. The errors
occur on all thinkable usercontrols and pages. There is no special code
fragment which raise the error.
But this behavior occurs only if i the app has a very high load. (i
simulated this on my dev box with the application test center).

I searched for "global variables" to try another approach but i found
nothing really usefull.

Now i'm thinking of
a) using a session instead of a static to hold the instance of this class or
b) to create a instance of this class in the page_load/prerender event of a
all my pages and make this instance accessable to the usercontrols with a
property.

or c) i'm completly wrong all the time and there's another/better way to
access "global variables" accross a page and its usercontrols.

Any suggestions?

Thanks
 
H

Hans Kesting

Göran Tänzer said:
Hi,

i've written a class which does some calculations for my web
application. These informatinos are different for each page request -
the current user is not important.

i have about 10 aspx pages and 20 ascx user controls. In most of these
pages/user controls i need the informations of this class.

First i created an instance of this class in every page/user control i
needed the information with the result that a page with 10
usercontrols calculated the same information 11 times until the
request is completed (=many redundant/identical calculations).

So i created a static public instance of this class in the global.asax
Application_PreRequestHandlerExecute event. The class is now created
just once per page request and i can access it from the page and
usercontrols like Global.MyClass.MyPoperties.

But i'm not happy with this because i'm getting "accidentally" errors
like "Object has been disposed" when i access properties of this
class. The errors occur on all thinkable usercontrols and pages.
There is no special code fragment which raise the error.
But this behavior occurs only if i the app has a very high load. (i
simulated this on my dev box with the application test center).

I searched for "global variables" to try another approach but i found
nothing really usefull.

Now i'm thinking of
a) using a session instead of a static to hold the instance of this
class or b) to create a instance of this class in the
page_load/prerender event of a all my pages and make this instance
accessable to the usercontrols with a property.

or c) i'm completly wrong all the time and there's another/better way
to access "global variables" accross a page and its usercontrols.

Any suggestions?

Thanks

One thing to remember when using static members, is that that same
value is available for the entire (web-)application, that is: for all users
that are requesting pages.

You could use "Session" to store it:
If I understand correctly, you can calculate the value in the page's
Page_Init (or Page_Load) and store it in Session, the controls can
then read it in the PreRender.

Hans Kesting
 
K

Kevin Spencer

It's difficult to understand what your requirements for this class are, as
you haven't been very specific about them ("does some calculations for my
web application"). It SOUNDS like you may have a class which performs
process only ("calculations"). If that is the case, no instance of a class
is necessary. Static methods (not static classes) do not require an instance
of a class to execute. For example, the System.Math class has numerous
methods for performing calcuations. Note that you never have to create an
instance of this class to use its methods; they are static. So, what you
should be doing, if I understand you correctly, is simply to create a public
class with static members, and then simply use these static memebers without
an instance of the class.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
The sun never sets on
the Kingdom of Heaven
 
G

Guest

Hmm ok,

this class is calculating some informations i need to work with my (M)CMS;
lots of recursive stuff for navigation and multi language support, images and
so on. If i access for example www.mysite.com/page.htm i need a lot of basic
informations from my CMS in the aspx and ascx files to create this page.
If i access the same public static method 5 times in my aspx page and 3
times in 10 usercontrols within this aspx page i would calculate the same
information 35 times.
So i think its better to create one instance of this class which holds all
the needed informations to create page.htm.
 
K

Kevin Spencer

Well, again, I'm not sure what this "CMS" is, nor how it works. If you need
an instance of a class on a per page basis, It would be best to use a
(non-static) business class that you simply instantiate in the page. If you
want to cache the business class, cache it in Session or Application.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
The sun never sets on
the Kingdom of Heaven
 
K

Kevin Spencer

Hi Juan,
CMS = Content Management System

I know that much! However, the term is generic, and does not imply any real
information about how the particular CMS works. It only implies that some
application manages some content. And there are plenty of them around. They
come in all shapes and sizes.

--

Kevin Spencer
Microsoft MVP
..Net Developer
The sun never sets on
the Kingdom of Heaven
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top