Adding global objects to a web application

  • Thread starter Thread starter Andy B
  • Start date Start date
A

Andy B

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?
 
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/
======================================
 
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 said:
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 said:
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?
 
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 said:
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 said:
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 said:
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?
 
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 said:
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 said:
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 said:
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


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?
 
Back
Top