Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Adding global objects to a web application

1 view
Skip to first unread message

Andy B

unread,
Apr 29, 2008, 2:41:13 AM4/29/08
to
I want to have page level access to a certain object but cant figure out how
to do this. I create a class method that returns the object, but I cant get
it to be global to the page. By global to the page, I mean to let all
methods and event handlers have access to the same instance of the object. I
looked at a global.asax file, but that doesn't seem to be what I am looking
for (since the object is supposed to be page wide not application wide). Any
ideas how I can do this?

Juan T. Llibre

unread,
Apr 29, 2008, 3:11:43 AM4/29/08
to
Place your object's class in the App_Code folder,
or compile an assembly from the source class
and place it in the /bin directory of your application.


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
"Andy B" <a_b...@sbcglobal.net> wrote in message news:u81MBQcq...@TK2MSFTNGP04.phx.gbl...

Eliyahu Goldin

unread,
Apr 29, 2008, 5:59:20 AM4/29/08
to
Perhaps I am missing something. Why don't you just declare a reference to
the oblect:

private MyClass myObject;

in the page class?

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


"Andy B" <a_b...@sbcglobal.net> wrote in message
news:u81MBQcq...@TK2MSFTNGP04.phx.gbl...

Andy B

unread,
Apr 29, 2008, 9:53:36 AM4/29/08
to
I have this code:
//this code works in the page class
//Create a ContractManager so we can have an object that will create, save,
convert the object and other things as well. This I would like to be web
application
//global. I.e. once the instance is created for the whole application, it
doesn't need to be recreated.
ContractManager Manager = new ContractManager();

//Once the ContractManager is created, I now need to create the object
itself. This requires the following code which wont work inside a page class
since it is
//executable code. This is where I run into huge problems since the
following object needs to be accessible by all of the methods/event handlers
of the class during
//its entire lifecycle.
//This creates the object, assigns default values and returns a Contract
object and assigns it to StockContract.
Contract StockContract = Manager.CreateEmptyStockContract();

Hope this helps with what I need.


"Eliyahu Goldin" <REMOVEALLCAPIT...@mMvVpPsS.org> wrote in
message news:%23NgEg$dqIHA...@TK2MSFTNGP04.phx.gbl...

bruce barker

unread,
Apr 29, 2008, 12:10:00 PM4/29/08
to
as long as you realize the page lifecycle is for only one request, as a new
page object is created per request, just create a class variable:

private Contract StockContract = Manager.CreateEmptyStockContract();

this will be available to all methods in the page class. if you need the
same instance on a postback, then you will need to come up up a way to
save/restore the object between postbacks. the session object can help here.


-- bruce (sqlwork.com)

Andy B

unread,
Apr 29, 2008, 12:50:38 PM4/29/08
to
Contract StockContract = Manager.CreateEmptyStockContract();

Doesn't actually work in just a class. I get a compiler error saying that
this statement is invalid unless inside a method of some kind. I will need
the StockContract object available to all methods/event handlers and it will
have to be around during postbacks.


"bruce barker" <bruce...@discussions.microsoft.com> wrote in message
news:86ECEC89-012B-4656...@microsoft.com...

0 new messages