Constructor call constructor

G

Geoffrey

Hello,



I wan't to have optionnal parameter in my cronstructor, so, I define 2
constructors.

when I call the constructor without the "optional" parameter, I want to call
the constructor with complete parameteers list.

Like this : (but it's no good)

public class SystemEvent

{

public SystemEvent(DateTime p_Heure, String p_Porte, String p_TypeEvt)

{

SystemEvent(p_Heure, p_Porte, p_TypeEvt,false,true);

}

public SystemEvent(DateTime p_Heure,String p_Porte,String p_TypeEvt,bool
p_FromLog,bool p_WithVerifEvent)

{

.....

}
 
G

Guest

It's not working.
You must create a private methode with the total parameter list and call
this methos from all constructors.

Mihaly
 
A

amaca

You can do it either way round with constructor chaining:


public class SystemEvent
{

public SystemEvent(DateTime p_Heure, String p_Porte, String p_TypeEvt)
{
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 the
smaller parameter list calling the one with the larger, with default values.
This generates efficient code; calling common methods doesn't.

Andrew
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

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 not
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)
{}

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;
}
 
R

Rick Lones

Geoffrey said:
I wan't to have optionnal parameter in my cronstructor, so, I define 2
constructors.

when I call the constructor without the "optional" parameter, I want to call
the constructor with complete parameteers list.

Like this : (but it's no good)

public class SystemEvent

{

public SystemEvent(DateTime p_Heure, String p_Porte, String p_TypeEvt)

{

SystemEvent(p_Heure, p_Porte, p_TypeEvt,false,true);

}

public SystemEvent(DateTime p_Heure,String p_Porte,String p_TypeEvt,bool
p_FromLog,bool p_WithVerifEvent)

{

....

}

Geoffrey,

Try:

public class SystemEvent
{
public SystemEvent(DateTime p_Heure, String p_Porte, String p_TypeEvt) :
this(p_Heure, p_Porte, p_TypeEvt, false, true)
{
}

public SystemEvent(DateTime p_Heure,String p_Porte,String p_TypeEvt,
bool p_FromLog,bool p_WithVerifEvent)
{
// your code here . . .
}
}


HTH,
-rick-
 
G

Geoffrey

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
 
B

Bruce Wood

Geoffrey said:
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

I agree. I have tried both ways and I find it much clearer putting all
of the code in the constructor with the most parameters and then
setting up the other constructors as "shortcuts" to it.

The only problem comes when you introduce inheritance where the child
class also has the same "shortcut" scheme. It's a toss-up as to whether
the child class should do the same as the parent, duplicating the
default values for the optional arguments, or call the parent's
"shortcut" constructors, thus duplicating the code that initializes
fields in the child.

I still haven't figured out a clean way to handle that one. Any takers?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top