Good day!
In case you have two constructors, for example..
public class Test
{
public Test(object o)
{
if(/* blah.. condición */)
//continuar en el otro constructor
}
public Test()
{
/*Configuraría ciertos parámetros en caso de que
el parámetrod e la función anterior fuese válido.
*/
}
}
Would it be possible, from the first constructor, Test(object o)
based on a certain condition, to continue in the constructor Test()
?
I know that the most consistent thing to do would be to decouple the code from Test()
a function and call it from both constructors if necessary.
But the case occurred and the doubt arose, is it possible?
You cannot call a constructor based on a condition because it would violate the rule that the first to be executed is the constructor.
I see 2 ways how you can achieve what you want.
1- You pass a boolan expression to the constructor from the second using
this
and thus decide whether or not to execute the code in the first:2- You put the code of the first constructor in a method and do the condition in the second: