Re: Constructor call constructor
- From: "Geoffrey" <AEnlevergeoffrey@xxxxxxxxx>
- Date: Tue, 4 Apr 2006 17:27:09 +0200
Personnaly, I choose to place all the code in the constructor with all the
parameters.
The constructors with minder parameters are only "shortcuts" for the full
constructor
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> a
écrit dans le message de news:euHRrh%23VGHA.424@xxxxxxxxxxxxxxxxxxxxxxx
Hi,not
IMO it's better to concentrate all the code in only one constructor,
otherwise you may get errors as you include some code in one variant and
the other, cause remember that you usually do more than just assign values:
to variables.
In your example, what happens with "FromLog " if the first constructor is
called?
because of that yours contructor with partial parameters should call the
constructor with all the parameters and there you do your init:
public SystemEvent(DateTime p_Heure, String p_Porte, String p_TypeEvt)
this (p_Heure, p_Porte, p_TypeEvt, false, false)p_TypeEvt)
{}
public SystemEvent(DateTime p_Heure,String p_Porte,String p_TypeEvt,
bool p_FromLog,bool p_WithVerifEvent) {
this.p_FromLog = p_FromLog;
this.p_WithVerifEvent;
this.p_Heure = pHeure;
this.p_Porte = p_Porte;
this.p_TypeEvt = p_TypeEvt;
}
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"amaca" <public@xxxxxxxx> wrote in message
news:uGmK5U9VGHA.4724@xxxxxxxxxxxxxxxxxxxxxxx
You can do it either way round with constructor chaining:
public class SystemEvent
{
public SystemEvent(DateTime p_Heure, String p_Porte, String
the{
this.p_Heure = pHeure;
this.p_Porte = p_Porte;
this.p_TypeEvt = p_TypeEvt;
}
public SystemEvent(DateTime p_Heure,String p_Porte,String p_TypeEvt,
bool p_FromLog,bool p_WithVerifEvent) : this (p_Heure, p_Porte,
p_TypeEvt)
{
this.p_FromLog = p_FromLog;
this.p_WithVerifEvent;
}
}
Or you can do it the way round you have it, with the constructor with
smaller parameter list calling the one with the larger, with default
values. This generates efficient code; calling common methods doesn't.
Andrew
.
- Follow-Ups:
- Re: Constructor call constructor
- From: Bruce Wood
- Re: Constructor call constructor
- References:
- Constructor call constructor
- From: Geoffrey
- Re: Constructor call constructor
- From: amaca
- Re: Constructor call constructor
- From: Ignacio Machin \( .NET/ C# MVP \)
- Constructor call constructor
- Prev by Date: Re: Delegation question ?
- Next by Date: PLS HELP! Override default event behavior (add/remove)
- Previous by thread: Re: Constructor call constructor
- Next by thread: Re: Constructor call constructor
- Index(es):
Relevant Pages
|