2 initializers in 1 constructor

G

GeezerButler

We have 2 classes. Class B derives from A

//Has 2 constructors
public class A
{
A()
{
//do something
}
A(string name)
{
//do something
}
}

//and B has 3 constructors
public class B : A
{
B()
{
//do something
}
B(int age)
{
//do something
}
B(string name, int age)
{
//do something
}
}

In the third contructor of B, i want to call base(name) and also
this(age).
i.e. B(string name, int age) : base(name) works
B(string name, int age) : this(age) works
but B(string name, int age) : this(age) , base(name) does not work

Is it possible to do this in some way?
 
E

Eugene

DotNet???????????
It allows only one father class to Any inherited object in DotNet
 
J

Jon Skeet [C# MVP]

GeezerButler said:
We have 2 classes. Class B derives from A

In the third contructor of B, i want to call base(name) and also
this(age).
i.e. B(string name, int age) : base(name) works
B(string name, int age) : this(age) works
but B(string name, int age) : this(age) , base(name) does not work

Is it possible to do this in some way?

No, not directly. The better way to do this is usually to do it the
other way round - make B(int age) call B(null, age) (or some other
default value) and make B(string name, int age) call base(name).

Basically, whatever you do you'll have a chain of constructors, some in
the derived class and then ultimately one calling the base class
constructor, using a parameterless constructor implicitly if you don't
specify anything.
 
N

narayanareddy.tera

Hi GeezerButler,

the dot not compiler does allow us to invoke 2 constuctors(whether the
two are of it's own or it's parent).you can invoke only one either
of it's own or it's parent.
Here the reason is, if it allows then 2 objects of that class will be
instantiated (one by each constructor invoke) .one of the two objects
would become unreached oblect.

So it is not passible to do like that :)

Cheers
Naren
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Hi GeezerButler,

the dot not compiler does allow us to invoke 2 constuctors(whether the
two are of it's own or it's parent).you can invoke only one either
of it's own or it's parent.

No really, you have to invoke one of the constructor defined by the current
class, is this constructor the one that call the parent.
Here the reason is, if it allows then 2 objects of that class will be
instantiated (one by each constructor invoke) .one of the two objects
would become unreached oblect.

I do not understand the above sentence, do you care elaborate it further?
 
B

Ben Voigt

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,



No really, you have to invoke one of the constructor defined by the
current class, is this constructor the one that call the parent.


I do not understand the above sentence, do you care elaborate it further?

When you call a sibling constructor, it calls a base constructor. If you
called the base constructor again yourself, you'd now have two live
instances of the base class (possibly the second overwrote the memory of the
first). In any case, the derived class should only have one base
sub-object, the other would become unreachable.
 
J

Jon Skeet [C# MVP]

Ben Voigt said:
When you call a sibling constructor, it calls a base constructor. If you
called the base constructor again yourself, you'd now have two live
instances of the base class (possibly the second overwrote the memory of the
first).

No, you wouldn't. Constructors don't create instances - otherwise when
one constructor called another which called another, etc, you'd be
creating lots of instances. Constructors *initialise* objects.
In any case, the derived class should only have one base
sub-object, the other would become unreachable.

There is no "sub-object". The object is an instance of both the derived
class and the base class. It's not an instance of a derived class has a
reference to a separate instance of the base class.
 
G

GeezerButler

No, not directly. The better way to do this is usually to do it the
other way round - make B(int age) call B(null, age) (or some other
default value) and make B(string name, int age) call base(name).

Hi Jon,
I'm not too sure if i understand what this would achieve.
making B(string name, int age) call base(name) still does not call
this(age).

Anyway, i read on msdn that only 1 initializer is allowed per ctor, so
i guess i'll just copy paste the code in B(int age) to B(name, age)
and make B(name, age) call base(name).

About the 1 initializer is allowed per ctor thing, could someone shed
some light on why this is necessary?

Thanks.
 
J

Jon Skeet [C# MVP]

GeezerButler said:
I'm not too sure if i understand what this would achieve.
making B(string name, int age) call base(name) still does not call
this(age).

No, but you'd put the logic of B(age) into B(name, age) so that
everything appropriate would still be called.
Anyway, i read on msdn that only 1 initializer is allowed per ctor, so
i guess i'll just copy paste the code in B(int age) to B(name, age)
and make B(name, age) call base(name).

Don't have the same code in two places - move it into B(name,age) and
make B(age) call B(name,age).
About the 1 initializer is allowed per ctor thing, could someone shed
some light on why this is necessary?

Couldn't say off the top of my head.
 
B

Bruce Wood

Hi Jon,
I'm not too sure if i understand what this would achieve.
making B(string name, int age) call base(name) still does not call
this(age).

What Jon is getting at is that the "usual" idiom for writing multiple
constructors is this:

1. There is one constructor that takes all of the necessary
parameters. It's the one that contains the code to initialize the
object.

2. The other constructors, the ones with fewer parameters, call the
constructor that you built in step 1, passing default values for the
missing arguments.

So, in your example, you have a base class A with two constructors,
one that takes an age, and one that takes no parameters. The second
would call the first, passing some default value for "age":

public class A
{
public A() : this(0) { }

public A(int age)
{
// initialize object here
}
}

You would then do the same for B:

public class B
{
public B() : this("", 0) { }
public B(int age) : this("", age) { }

public B(string name, int age) : base(age)
{
// initialize object here
}
}

So, when you call new B(15), it ends up calling the constructor B("",
15), which ends up calling A(15).
 

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