locked
Is using class level private shared variables not good? RRS feed

  • Question

  • User-245554983 posted

    Hi,

    Thanks for your attention and sparing precious time, I need your help please.

    Snario:

    In page behind classes I am using a private and shared object (list<client> or just client) to temporary hold data coming from database or class library. This object is used temporarily to catch data and than to return, pass to some function or binding a control.

    1st: Can this approach harm any way ? I couldnt analyse it but a thought was using such shared variables may replace data in it ?

    2nd: Please comment also on using such variables in BLL (to hold data coming from database) where new object of BLL class will be made everytime.

    Here is sample code for this:

    public class ClientManager
        {
            Client objclient = new Client();  //Used in 1st and 2nd method
            List<Client> clientlist = new List<Client>();// used in 3rd and 4th method
            ClientRepository objclientRep = new ClientRepository();
    
            public List<Client> GetClients()
            {
                return clientlist = objclientRep.GetClients();
            }
            public List<Client> SearchClients(string Keyword)
            {
                return clientlist = objclientRep.SearchClients(Keyword);
            }
    
            public Client GetaClient(int ClientId)
            {
                return objclient = objclientRep.GetaClient(ClientId);
            }
    
            public Client GetClientDetailForConfirmOrder(int UserId)
            {
                return objclientRep.GetClientDetailForConfirmOrder(UserId);
            }
    
        }
    


    Friday, May 28, 2010 2:54 AM

Answers

  • User-1179452826 posted

    As long as they're not static, it's not a problem. You are however wasting some constructor calls. For example:

    List<Client> clientList = new List<Client>();

    you could've just done:

    List<Client> clientList = null;

    since you're assigning a List<Client> to clientList in GetClient(), your newed up instance would be unreferenced and simply garbage collected when clientList is assigned in GetClient().

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 28, 2010 4:55 AM
  • User-1179452826 posted

    An instance of page is created to serve each request. If you have the property specified as static, it's shared among all instances of the class (and thus, shared among all requests). If it's not static, then it's a different property for each instance (and thus, each user). 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 28, 2010 7:02 AM
  • User-2004844803 posted

    No worries,

    A simplified version: Each request are internally handled by IIS using a pool of HttpApplication objects and each incomming request are handled by an instance fetched from this pool. If you have many reqests arriving at the same time they will all be handled by thier own HttpApplication object assigned to the request. The HttpApplication tries to locate a compiled version off the page to be handled, if no compiled version is found it will compile the page on the fly and create an instance off it and use it to process the request. So, just as HeartattacK wrote there wont be any problems if you are not using static variables. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 28, 2010 7:09 AM

All replies

  • User-1179452826 posted

    As long as they're not static, it's not a problem. You are however wasting some constructor calls. For example:

    List<Client> clientList = new List<Client>();

    you could've just done:

    List<Client> clientList = null;

    since you're assigning a List<Client> to clientList in GetClient(), your newed up instance would be unreferenced and simply garbage collected when clientList is assigned in GetClient().

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 28, 2010 4:55 AM
  • User-245554983 posted

    Hello HeartattacK,

    I am very thankful to have answer from you. Thanks again.

    After your replay a point is here to discuss.

    Please consider using such way in page behind cs file (page's code file). As per my understanding we dont make an object of page behind class and it is not session dependant (please guide if I am misunderstanding).

     In this case there will only one instance of object say clint. Now if in a single time multiple people access a page which uses this object will not values be lost or over witten ? Just as with the use of static members in static class ?

    Suppose my page has List<Client> Clients =new List<Client> on class level.

    On page load getting all client function, search client funtion and new clients funtion are accessing it in a single page. Consider multiple people accessing this page. Will a class levle shared member a problem ? It is ok or I should use seprate members in funtions.

    thanks once again.

     

     

    Friday, May 28, 2010 6:19 AM
  • User-1179452826 posted

    An instance of page is created to serve each request. If you have the property specified as static, it's shared among all instances of the class (and thus, shared among all requests). If it's not static, then it's a different property for each instance (and thus, each user). 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 28, 2010 7:02 AM
  • User-2004844803 posted

    No worries,

    A simplified version: Each request are internally handled by IIS using a pool of HttpApplication objects and each incomming request are handled by an instance fetched from this pool. If you have many reqests arriving at the same time they will all be handled by thier own HttpApplication object assigned to the request. The HttpApplication tries to locate a compiled version off the page to be handled, if no compiled version is found it will compile the page on the fly and create an instance off it and use it to process the request. So, just as HeartattacK wrote there wont be any problems if you are not using static variables. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 28, 2010 7:09 AM