G
Guest
I'm writing a family of classes which all inherit most of their methods and
code (including constructors) from a single base class. When attempting to
instance one of the derived classes using parameters, I get CS1501 (no method
with X arguments). Here's a simplified example which mimics the
circumstances:
namespace InheritError {
// Random base class.
public class A {
protected int i;
// Random contructor w/o parameters.
public A() {
this.i = 5;
}
// Random contructor *with* parameters.
public A(int initial) {
this.i = initial;
}
}
// Random derived class.
public class B : A {
public int Inc() {
// Instantiation of derived class using base class constructor w/
parameters.
return new B(this.i + 1); // CS1501
}
}
}
(I'm not normally calling the constructor from inside one of the class'
methods; that's just to make the code more compact.)
How can I make the derived classes correctly inherit the base constructors?
I'm aware that setting up "public B() : base()" constructors would allow
these calls to work, but adding a half-dozen different codeless constructors
to each of about two dozen derived classes will make the code both uglier and
harder to work with. e.g. Having to change twenty-five source files if I
need to add a new constructor instead of just one is just plain silly when
twenty-four of them contain no code in the constructors, plus I will be
making these classes available to outside developers and accidental omission
of one or more constructors could produce some difficult-to-locate bugs.
Surely there's another way to do this?
code (including constructors) from a single base class. When attempting to
instance one of the derived classes using parameters, I get CS1501 (no method
with X arguments). Here's a simplified example which mimics the
circumstances:
namespace InheritError {
// Random base class.
public class A {
protected int i;
// Random contructor w/o parameters.
public A() {
this.i = 5;
}
// Random contructor *with* parameters.
public A(int initial) {
this.i = initial;
}
}
// Random derived class.
public class B : A {
public int Inc() {
// Instantiation of derived class using base class constructor w/
parameters.
return new B(this.i + 1); // CS1501
}
}
}
(I'm not normally calling the constructor from inside one of the class'
methods; that's just to make the code more compact.)
How can I make the derived classes correctly inherit the base constructors?
I'm aware that setting up "public B() : base()" constructors would allow
these calls to work, but adding a half-dozen different codeless constructors
to each of about two dozen derived classes will make the code both uglier and
harder to work with. e.g. Having to change twenty-five source files if I
need to add a new constructor instead of just one is just plain silly when
twenty-four of them contain no code in the constructors, plus I will be
making these classes available to outside developers and accidental omission
of one or more constructors could produce some difficult-to-locate bugs.
Surely there's another way to do this?