>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
Voir le fil de ce thread
Voir le fil de ce thread à partir de ce message seulement
Voir tous les messages de ce thread
Voir tous les messages de ce thread à partir de ce message seulement