473,473 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Constructor call constructor

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)

{

.....

}
Apr 4 '06 #1
7 5586
It's not working.
You must create a private methode with the total parameter list and call
this methos from all constructors.

Mihaly
"Geoffrey" wrote:
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)

{

.....

}

Apr 4 '06 #2
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


Apr 4 '06 #3
Ok, thx
"amaca" <pu****@ajam.net> a écrit dans le message de
news:uG**************@TK2MSFTNGP09.phx.gbl...
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

Apr 4 '06 #4
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;
}

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"amaca" <pu****@ajam.net> wrote in message
news:uG**************@TK2MSFTNGP09.phx.gbl...
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

Apr 4 '06 #5
Geoffrey wrote:
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-
Apr 4 '06 #6
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:eu***************@TK2MSFTNGP12.phx.gbl...
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;
}

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"amaca" <pu****@ajam.net> wrote in message
news:uG**************@TK2MSFTNGP09.phx.gbl...
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


Apr 4 '06 #7
Geoffrey wrote:
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?

Apr 4 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
24
by: slurper | last post by:
i have the following class sequence { public: sequence (const sequence& mysequence, const int newjob) { job_sequence(mysequence.job_sequence) job_sequence.push_back(newjob); ... }
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
9
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.