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)
{
.....
} 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)
{
.....
}
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
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
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
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-
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
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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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) .
|
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);
...
}
|
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...
|
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?
...
|
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...
| |
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |
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...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |