Click here to Skip to main content
16,022,538 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hello all,

I want to know, which is the better methods to create a object of class on a code behind page.

##################################################################

Method 1.

I will create a objects at one time when user logs in to the system, in Global.ascx on Session_Start event. This will help me to create the object at once for that session.

Method 2.

I'll create a object when ever I want to use that class's methods. This means I'll create the object only when I want to use that class's methods.

##################################################################

Please let me know which is the better method in this or tell me which is the better other than these too.

Thanks,

Yogesh
Posted
Comments
Varun Sareen 17-Feb-12 0:46am    
Dear Friend, both are good in their own way. It totally depends on your requirement.
Sergey Alexandrovich Kryukov 21-Feb-12 2:05am    
Not really, because this is ASP.NET. The objects are only created once per each HTTP request. You cannot create object and use it on the other page. On the other page, you actually create the clone of the object out of session data or any persistent container (database, for example). So, whatever you do, the objects are created over and over in each page.

As to the second way, it does not make full sense. One uses not just the methods, but each instance method needs an instance. Maybe this is just poorly formulated. Lazy pattern makes sense. However, if the methods are static, object is not used, so it is not needed; if the method is the instance method, the object is needed, but the value of the class is in both instance and method; you need some data to create an instance, so it is not arbitrary created when needed.
--SA
Yogesh Gulve 22-Feb-12 2:59am    
Thanks SAKryukov for your valuable reply.

Could you tell me which is the better methods other than this for object creation which will help me to increase my application performance, please?
Varun Sareen 24-Feb-12 11:37am    
the explanation given clearly shows that you can use either one..depends on your choice..:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900