I am having the following problem:
public class A {
public A(X, Y, Z) {
...
}
}
public class B : A {
public B(X, Y) : base(X, Y) {
//i want to instantiate Z here and only then pass it to the base class!
}
}
How can I solve this problem? Is there a way?
The common solution is to call a static method belonging to the type that can calculate the value of the parameter to be passed to the base constructor.
For example:
Do note that
CalculateZcannot be an instance method, because thethisreference is not available in constructor initializers.From the language-specification 10.11.1 Constructor initializers:
EDIT: Changed ‘instance’ to ‘static’ in the description.