locked
public object on asp,net page RRS feed

  • Question

  • User687630469 posted

    in asp.net can i make public objects on my page, for example

    public Customer cust1 = new customer();

    and use this object on my entire code???


    what are the drawbacks of creating public objects??

    a single object will be created for all users??? or each user will have a different object????

    Friday, August 27, 2010 3:40 AM

Answers

  • User-2058999863 posted

    Hi,

    Please remember that ASP.NET applications are stateless, meaning that your objects will be disposed as soon as the page lifecycle ends.

    You could use "static" objects but please remember that in case IIS recycles your application pool, you will lose the data stored in your static objects.

    If you question is only creating a public object in an ASP.NET page and using it through the lifecycle of that page, it is no problem. As long as it is not a "public static" property, it will not be shared across different requests and each visitor will have its own instance of the public object.


    BR.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 27, 2010 4:22 AM
  • User-68639941 posted

    hi,

    it depends on your requirement , if you are going to use the object throughout the page you can create a global object and use it everywhere instead of creating new objects in all the places.

    and the object will be created for each and every request  of the page, it's not depend on users.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 27, 2010 4:30 AM

All replies

  • User-2058999863 posted

    Hi,

    Please remember that ASP.NET applications are stateless, meaning that your objects will be disposed as soon as the page lifecycle ends.

    You could use "static" objects but please remember that in case IIS recycles your application pool, you will lose the data stored in your static objects.

    If you question is only creating a public object in an ASP.NET page and using it through the lifecycle of that page, it is no problem. As long as it is not a "public static" property, it will not be shared across different requests and each visitor will have its own instance of the public object.


    BR.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 27, 2010 4:22 AM
  • User-68639941 posted

    hi,

    it depends on your requirement , if you are going to use the object throughout the page you can create a global object and use it everywhere instead of creating new objects in all the places.

    and the object will be created for each and every request  of the page, it's not depend on users.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 27, 2010 4:30 AM