inherited constructors

A

Alexander Muylaert

Hi

Is their a way to keep constructors visible in inherited classes?


public class X{
public x(int A){}
}

public class Y : X{};


Y MyVar = new Y(12);
The compiler is always nagging that their is no constructor...
Once define I would like to have it in inherited classes to.

Kind regards

Alexander
 
R

RENE STEIN

(Sun, 11 Jul 2004 20:27:28) about "inherited constructors":

Constructors are never inherited. This is by design. You must explicitly
call desired base constructor in derived classes.



public class Y : X
{
public Y(int A) : base(A) {
}

}

HTH
RENE STEIN <[email protected]>, C# MVP Sun, 11 Jul 2004 20:53:12 +0200

=== Posted with Qusnetsoft NewsReader 2.2.0.1

----- Original Message -----
From: "Alexander Muylaert" <[email protected]> Sent: Sun, 11 Jul 2004
20:27:28 Subject: inherited constructors


Hi

Is their a way to keep constructors visible in inherited classes?


public class X{
public x(int A){}
}

public class Y : X{};


Y MyVar = new Y(12);
The compiler is always nagging that their is no constructor...
Once define I would like to have it in inherited classes to.

Kind regards

Alexander
 
L

Lars Wilhelmsen

Hi Alexander,

Alexander Muylaert said:
Is their a way to keep constructors visible in inherited classes?

The answer to you question: No.
Why: A constructor should concentrate on initialising a new instance of the
given class - and that is the reason why ctors is not inherited in the
ususal way.

Try:

public class X
{
public X(int A){}
}

public class Y : X
{
public Y(int A)
: base(A) {}
}

Y MyVar = new Y(12);

Regards,

Lars Wilhelmsen
Software Engineer
Teleplan A/S, Norway.
 

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