Omer van Kloeten's .NET Zen

Programming is life, the rest is mere details

News

Note: This blog has moved to omervk.wordpress.com.

Omer van Kloeten's Facebook profile

Omer has been professionally developing applications over the past 8 years, both at the IDF’s IT corps and later at the Sela Technology Center, but has had the programming bug ever since he can remember himself.
As a senior developer at NuConomy, a leading web analytics and advertising startup, he leads a wide range of technologies for its flagship products.

Get Firefox


powered by Dapper 

.NET Resources

Articles :: CodeDom

Articles :: nGineer

Culture

Projects

Base Constructor Calls With Out Parameters

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 MyBaseClass
{
    public MyBaseClass(out int myParameter)
    {
        // Do initialization on myParameter.
        // Do magic...
    }
}
This class takes one parameter to its instance constructor, which is an out parameter.
Instace creation may look like this:
int i;
MyBaseClass myInstance = new MyBaseClass(out i);
Everything's peachy, Right?

Now take the class MyDerivedClass which derives from MyBaseClass:

public class MyDerivedClass : MyBaseClass
{
    private int m_MyInteger;

    public MyDerivedClass() : base(out m_MyInteger)
    {
        // Do magic...
    }
}
Oh, 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!
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.]

Comments

No Comments