Search code examples
c#oopabstract-classderived-classbase-class

OOP - How to demand derived classes to set a value to base attribute?


The output of my program is a table in a file.

Some users like the output as Excel file, the others prefer CSV, and so on.
I therefore have an abstarct class Document with 2 derived classes:
ExcelDocument and CsvDocument.

The delimiter between the output results is different in each class:
In the CSV file the delimiter is , and in the Excel the delimiter is /t.

How can I force the derived classes to set value to the delimiter attribute of the abstract Document class?


Solution

  • Use an abstract property:

    public abstract char Delimiter { get; }
    

    Then in your derived class:

    public override char Delimiter
    {
        get { return '\t'; }
    }