469,133 Members | 1,016 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,133 developers. It's quick & easy.

class instance

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
Nov 19 '05 #1
7 1486
Gran Tnzer wrote:
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
Nov 19 '05 #2
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

"Gran Tnzer" <Gr*******@discussions.microsoft.com> wrote in message
news:96**********************************@microsof t.com...
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

Nov 19 '05 #3
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.
"Kevin Spencer" wrote:
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öran Tänzer" <Gr*******@discussions.microsoft.com> wrote in message
news:96**********************************@microsof t.com...
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


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

"Gran Tnzer" <Gr*******@discussions.microsoft.com> wrote in message
news:E5**********************************@microsof t.com...
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.
"Kevin Spencer" wrote:
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

"Gran Tnzer" <Gr*******@discussions.microsoft.com> wrote in message
news:96**********************************@microsof t.com...
> 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


Nov 19 '05 #5
CMS = Content Management System

Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Espaol
Ven, y hablemos de ASP.NET...
======================

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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

"Gran Tnzer" <Gr*******@discussions.microsoft.com> wrote in message
news:E5**********************************@microsof t.com...
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.
"Kevin Spencer" wrote:
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

"Gran Tnzer" <Gr*******@discussions.microsoft.com> wrote in message
news:96**********************************@microsof t.com...
> 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


Nov 19 '05 #6
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

"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:OQ**************@tk2msftngp13.phx.gbl... CMS = Content Management System

Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Espaol
Ven, y hablemos de ASP.NET...
======================

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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

"Gran Tnzer" <Gr*******@discussions.microsoft.com> wrote in message
news:E5**********************************@microsof t.com...
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.
"Kevin Spencer" wrote:

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

"Gran Tnzer" <Gr*******@discussions.microsoft.com> wrote in message
news:96**********************************@microsof t.com...
> 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



Nov 19 '05 #7
re:
there are plenty of them around
Most are overpriced, too... ;-)

Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Espaol
Ven, y hablemos de ASP.NET...
======================

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:ee**************@TK2MSFTNGP15.phx.gbl... 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

"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:OQ**************@tk2msftngp13.phx.gbl...
CMS = Content Management System

Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Espaol
Ven, y hablemos de ASP.NET...
======================

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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

"Gran Tnzer" <Gr*******@discussions.microsoft.com> wrote in message
news:E5**********************************@microsof t.com...
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.
"Kevin Spencer" wrote:

> 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
>
> "Gran Tnzer" <Gr*******@discussions.microsoft.com> wrote in message
> news:96**********************************@microsof t.com...
> > 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
>
>
>



Nov 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Gabriel Genellina | last post: by
4 posts views Thread by Neil Zanella | last post: by
50 posts views Thread by Dan Perl | last post: by
18 posts views Thread by John M. Gabriele | last post: by
166 posts views Thread by Graham | last post: by
19 posts views Thread by Jamey Shuemaker | last post: by
20 posts views Thread by tshad | last post: by
44 posts views Thread by Steven D'Aprano | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.