2

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.

asked Nov 14, 2010 at 22:11

3 Answers 3

6

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?

answered Nov 14, 2010 at 22:24
Sign up to request clarification or add additional context in comments.

3 Comments

Din't think of putting the preperation in a seperate method, it's not ideal but it will work, thanks.
And depending on how complex it is, it could also be expressed inside the this() cpnstruction
Just a small note: PrepareArgument must be static in the second approach.
1

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.

answered Nov 14, 2010 at 22:16

2 Comments

Why are we talking about headers and C# in the same topic? (not you so much as the OP)
I meant the header of the constructor, ie. constructor chaining, and that should be the same he was talking about as well.
1

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;
 }
}
answered Nov 14, 2010 at 22:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.