5

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.

Dan Hall
  • 1,474
  • 2
  • 18
  • 43
  • 2
    Please show the base class `ExportData` Just add a default constructor to it – Gilad Green Nov 03 '16 at 15:36
  • 2
    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 bug – Panagiotis Kanavos Nov 03 '16 at 15:38
  • 1
    You can create empty protected constructor at ExportData class, then call it from ExtendedExportData (:base()). – Evk Nov 03 '16 at 15:40

5 Answers5

6

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(...).

C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72
  • That sorts it nicely, i wasn't aware a parameterless constructor was automatically created, that explains a lot now :) I've added the protected parameterless constructor to the base class and now all is well. – Dan Hall Nov 03 '16 at 15:46
1

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()
TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69
  • Thanks, It appears you're mostly correct but after further testing i've found that with the parameterless constructor in the base class you don't need to call : base() in the inherting class. – Dan Hall Nov 03 '16 at 15:48
  • @DanHall I found that out in the answer you accepted and so upvoted his accordingly but seeing as mine is also correct just more explicit I left it around – TheLethalCoder Nov 03 '16 at 16:10
  • Thanks again, nothing wrong with a little overkill code for clarity so yours was close to being the accepted answer. – Dan Hall Nov 03 '16 at 16:21
1

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

Else you could do something like here.

Community
  • 1
  • 1
Isaak
  • 66
  • 2
  • That does the trick nicely, i just added it as a protected constructor and left it empty, only for use when being inherited. Thanks. – Dan Hall Nov 03 '16 at 15:44
1

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#

wtct
  • 167
  • 1
  • 3
0

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.

renklus
  • 776
  • 6
  • 17