Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
When to instantiate a class
Message
De
10/02/2009 17:20:41
 
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
01380585
Message ID:
01380720
Vues:
28
>My general rule of thumb is based on how often it's needed and/or how expensive it is to initialize. For something like a business >object/rules class, it's probably going to be needed fairly often and from multiple places, so it gets created when the form inits (or >lazy load it via a property - create it in the get if necessary). For something like a report, I would probably wait until it's needed.

>If a var is used in a method it usually is cleared when it goes out of scope. Usually. It's really up to the garbage collector to determine >when that really happens, though.

>The other case is how expensive is it to init the class. If it does a lot of processing/grabbing data/etc. I will normally delay >instanciation as long as possible, but then keep the object around after init (see my suggestion about using a lazy loaded property).

>(by lazy loading, I mean this):

>private MyBizObj m_bizObj;

>public MyBizObj BizObj
>{
> get
> {
> if (this.m_bizObj == null)
> this.m_bizObj = new MyBizObj();

> return this.m_bizObj;
> }
>}
>-Paul

>RCS Solutions, Inc.
>Blog
>Twitter

Thanks Paul and all others. I'll give this some consideration.

Alan
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform