4
votes

I'm just after a little advice on how to best organise the inheritance here. I have a class (below) that needs to use most of, and overide some of, the classes in the base ExportData class:

public class ExtendedExportData : ExportData
{

    private tData _Data;
    private ResStrings _diaStrings;

    public ExtendedExportData(tData Data, ResStrings diaStrings)
        : base(tData, diaStrings)
    {
        _tData = Data;
        _diaStrings = diaStrings;

    }
}

What isn't neccesary in this case is having to call the base constructor as none of the base class methods used in the class above require that initialisation, the constructor is only neccesary when creating an instance of the base class directly for other (verbose) purposes.

If i remove the base constructor i get an error on the sub-class constructor saying the base constructor doesn't take 0 arguments. How can i avoid this?

Here's the base class:

public class ExportData
{
    private tData _Data;
    private ResStrings _diaStrings;

    public ExportLines(tData Data, ResStrings diaStrings)
    {
        _Data = Data;
        _diaStrings = diaStrings;
    }
}

Thanks in advance.

5
Please show the base class ExportData Just add a default constructor to itGilad Green
Your code does instantiate an instance of the base class. That's what inheritance means - ExtendedExportData is an instance of ExportData. If ExportData has fields that don't make sense for ExtendedExportData you have a design bugPanagiotis Kanavos
You can create empty protected constructor at ExportData class, then call it from ExtendedExportData (:base()).Evk

5 Answers

6
votes

A constructor of the base class is always used - but when the base class has no explicitly defined constructor, C# will generate a public parameterless constructor automatically.

Similarly, if you don't explicitly mention : base(...) in your derived class, C# assumes you want to use the parameterless constructor of the base class.

As soon as you define a constructor, the parameterless constructor is not automatically generated anymore, and so you have to call : base(...).

You can easily change this by adding such a constructor to the base class:

public class ExportData
{
    protected ExportData()
    {
    }
}

By making this constructor protected, only classes that inherit from ExportData can use this constructor, and they no longer have to include : base(...).

1
votes

You can always have a parameterless constructor in the base class that does all the initialising it needs to i.e.:

public ExportData()
{
    //Initialising here
}

And then in your example just call base() i.e.

public ExtendedExportData(tData Data, ResStrings diaStrings)
    : base()
1
votes

Couldn't you just create an empty constructor (public ExportData(){}) in ExportData class ?

Else you could do something like here.

1
votes

I know that this is old question but for future similar problem solution finders I would write something else.

If I undersand this question properly there is more elegant and common solution. Just make your base class abstract. You can still call base constructor during inheritance.

More information can be found here:

Constructor of an abstract class in C#

0
votes

The error is because the base class always has to be instantiated and your base class doesn't have a default constructor.

If you have functionality in your base class that's not needed in the base class review your inheritance architecture.