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
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