I’ve thought about this for a while, but I can’t figure out the answer. Maybe someone can help me with this.
One note before we start, I’d like to ask everyone to not comment about whether the design looks good or not, but on the specific language features that I find lacking.
Take the class MyBaseClass which is as follows:
public class MyBaseClassThis class takes one parameter to its instance constructor, which is an out parameter.
public MyBaseClass(out int myParameter)
// Do initialization on myParameter.
// Do magic…
Instace creation may look like this:
int i;Everything’s peachy, Right?
MyBaseClass myInstance = new MyBaseClass(out i);
Now take the class MyDerivedClass which derives from MyBaseClass:
public class MyDerivedClass : MyBaseClassOh, but wait. There’s a compiler error in there! You can’t pass an instance member to the call to the base constructor, even though it makes complete sense!
private int m_MyInteger;
public MyDerivedClass() : base(out m_MyInteger)
// Do magic…
What you can pass is a type (static) member.
Now, my question is this: Why can’t I pass an instance member as an out parameter to a call to a base constructor?
[Update: My original meaning was not why it was not allowed to send an instance member to the base constructor (I do understand that), but why it is not allowed to send an instance member specifically with an out modifier to the base constructor. There is no need for the value to be set into that member yet. Also, Ron Buckton has a good workaround for this, if anyone requires it.]