Constructor chaining order

D

Deckarep

Hey everyone,

I like using the technique of constructor chaining in other
languages. But something that tricks me up is the fact the whatever
constructor you specifiy to call from one constructor always gets
called first.

So for example:

private int myPrivateInt = 0;

public Constructor() <-- gets called first then returns execution
back to the calling constructor below
{
Console.WriteLine( myPrivateInt ); <---this prints 0 NOT 35 like I
want
}

public Constructor( int a ):this()
{
myPrivateInt = a;
}

//Somewhere else I instantiate my object like so:
Constructor myConstructor = new Constructor( 35 );

My problem is I would like the first Constructor with no parameters to
be able to see or get at the private int that I pass in. But, this is
not the case because the order is wrong.

So how can use this technique and allow the parameterless Constructor
to see the changes made in the calling constructor?

I'm sure this is another issue where I'm just thinking about it
wrong...but in another language for example I can call the other
constructor anywhere in the method I choose so I can garauntee to see
those values changed in the constructor that was called.

Okay, hopefully this makes sense.

Thanks folks!

-Ralph
 
N

Nicholas Paldino [.NET/C# MVP]

Deckarep,

To get around this, you have to reverse the calls so that your
parameterless constructors call your parameterized constructors, while
placing the logic from your parameterless constructor in the parameterized
version, like so:

private int myPrivateInt = 0;

public Constructor() : this(0)
{ }

public Constructor( int a )
{
myPrivateInt = a;

Console.WriteLine( myPrivateInt ); <---this prints 0 NOT 35 like I want
}

I think it's more rational to do this, because parameterless
constructors imply that you are defaulting some of the values which you are
passing to a parameterized constructor.

Hope this helps.
 
B

Ben Voigt

Deckarep said:
Hey everyone,

I like using the technique of constructor chaining in other
languages. But something that tricks me up is the fact the whatever
constructor you specifiy to call from one constructor always gets
called first.

So for example:

private int myPrivateInt = 0;

public Constructor() <-- gets called first then returns execution
back to the calling constructor below
{
Console.WriteLine( myPrivateInt ); <---this prints 0 NOT 35 like I
want
}

public Constructor( int a ):this()
{
myPrivateInt = a;
}

//Somewhere else I instantiate my object like so:
Constructor myConstructor = new Constructor( 35 );

My problem is I would like the first Constructor with no parameters to
be able to see or get at the private int that I pass in. But, this is
not the case because the order is wrong.

So how can use this technique and allow the parameterless Constructor
to see the changes made in the calling constructor?

Instead of calling to another constructor, call to a private member function
from both constructors.
I'm sure this is another issue where I'm just thinking about it
wrong...but in another language for example I can call the other
constructor anywhere in the method I choose so I can garauntee to see
those values changed in the constructor that was called.

Any language which allows that isn't respecting object lifetime rules.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top