How call constructor inside other constructor?
My class has quite some properties and one of my constructors sets them all, I want the default constructor to call this other one and use the set properties. But I need to prepare arguments first, so calling it from the header won't help.
Here's what I'd like to do:
public class Test
{
private int result, other;
开发者_高级运维 // The other constructor can be called from header,
// but then the argument cannot be prepared.
public Test() : this(0)
{
// Prepare arguments.
int calculations = 1;
// Call constructor with argument.
this(calculations);
// Do some other stuff.
other = 2;
}
public Test(int number)
{
// Assign inner variables.
result = number;
}
}
So this is not possible, does anyone know how to call my constructor to set parameters inside code? Currently I'm storing a copy of the object from the other constructor and copying all the properties, it's really annoying.
Yes. just pass the value 1 in the first call
public class Test
{
private int result, other;
public Test() : this(1)
{
// Do some other stuff.
other = 2;
}
public Test(int number)
{
// Assign inner variables.
result = number;
}
}
And why can't you prepare the argument?
public class Test
{
private int result, other;
public Test() : this(PrepareArgument())
{
// Do some other stuff.
other = 2;
}
public Test(int number)
{
// Assign inner variables.
result = number;
}
private int PrepareArgument()
{
int argument;
// whatever you want to do to prepare the argument
return argument;
}
}
or... if the prepareArgument is to be called from the default constructor, then it cannot be dependent on any passed in arguments. If it is a constant, just make it a constant ...
public class Test
{
const int result = 1;
private int result, other;
public Test()
{
// Do some other stuff.
other = 2;
}
}
if it's value is dependent on other external state, then you might rethink your design... Why not calculate it before calling the constructor in the first place?
Sounds like a little refactoring will help. While you can't do exactly what you want to do, there are always alternatives. I realize your actual code might be more complicated than the code you gave, so take this for an example refactoring of your problem. Extracting all common code into a new SetVariables
method.
public class Test
{
private int result, other;
// The other constructor can be called from header,
// but then the argument cannot be prepared.
public Test() : this(0)
{
// Prepare arguments.
int calculations = 1;
// Call constructor with argument.
SetVariables(calculations);
// Do some other stuff.
other = 2;
}
public Test(int number)
{
// Assign inner variables.
SetVariables(number);
}
private void SetVariables(int number)
{
result = number;
}
}
If you want to call a constructor, but can't do it from the header, the only way is to define a private Initialize
method. It appears you are doing "real work" in your constructor, which is not the task of a constructor. You should think of a different way to achieve your goal as your way severely prohibits debugging and others from reading your code.
精彩评论