How to do inhereted empty constructor when Base class has constructor with parameters

Azi

i.e. I've got a class Box with properties length, height, width and constructor "Box(int witdth, int height, int length)". Got an inhereted class ProductBox with property Name. I've also got a .csv file which I parse to ProductBox with FileHelpers library, so my constructor's empty. For now I can't inherete ProductBox from Box, because Box has not-empty constructor, and in FileHelpers the constructor's empty. Is there a way to inherete ProductBox from Box?

class Box
{
    int Width;
    int Height;
    int Length;

    public Box(int width, int height, int length)
    {
        this.Width = width;
        this.Height = height;
        this.Length = length;
    }
}

class ProductBox : Box
{
    string Name;
    int Width;
    int Height;
    int Length;
    public static ProductBox[] GetInfo(string filePath)
    {
        var engine = new FileHelperEngine<ProductBox>();
        var result = engine.ReadFile(filePath);
        return result;
    }
}
shamp00

From a design perspective, I think you are going about it the wrong way. While you might be able to hack something together to solve your constructor problem, you are trying to use the Box class for two incompatible uses. Instead you should have two separate classes.

FileHelpers is a library for describing csv files so that you can import them easily. You should have a BoxFileSpec for describing your file. It's really not a proper C# class - it may have: dummy fields to represent unused columns; lots of attributes [FieldNullValue], [FieldQuoted], [FieldConverter]; etc. It works best with public fields (a FileHelpers limitation which is not C# best practice), etc. It is a convenience syntax for describing the import file. It should not include any logic or special constructors.

Then you can have a clean best-practices Box class which has your specialized constructors and additional logic, properties, methods, whatever.

Then you use a FileHelperEngine<BoxFileSpec> to read the records into an array and map it to an enumerable of Box (via Linq or a library like AutoMapper).

Something like:

/// A 'real' class. Add methods, getters, setters, whatever.
/// FileHelpers doesn't use this class.
class Box
{
    public int Width { get; set; }
    public int Height { get; set; }
    public int Length { get; set; }

    public Box(int width, int height, int length)
    {
        this.Width = width;
        this.Height = height;
        this.Length = length;
    }
}

/// A 'real' class. Add methods, getters, setters, whatever.
/// FileHelpers doesn't use this class.
class ProductBox : Box
{
    public ProductBox(int width, int height, int length) 
        : base(width, height, length)
    { }

    public int Name { get; set; }
}

/// This is the class FileHelpers will use
/// This class describes the CSV file only. Stick to whatever
/// syntax conventions are required by FileHelpers.
[DelimitedRecord(";")]
class ProductBoxFileSpec
{
    [FieldQuoted(QuoteMode.OptionalForRead)]
    public int Width;
    [FieldQuoted(QuoteMode.OptionalForRead)]
    public int Height;
    [FieldQuoted(QuoteMode.OptionalForRead)]
    // Handle non-US formats such as , decimal points
    // convert from inches to centimetres? 
    // you get the idea...
    [FieldConverter(MyCustomizedLengthConverter)] 
    public int Length;
    [FieldOptional]
    public string SomeDummyExtraCSVColumn;
}

class Program
{
    static void Main(string[] args)
    {
        var engine = new FileHelperEngine<ProductBoxFileSpec>();
        var productBoxRecords = engine.ReadFile(filePath);
        var productBoxes = productBoxRecords
            .Select(x => new ProductBox(x.Width, x.Height, x.Length) { Name = x.Name });
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Inheritance with base class constructor with parameters

From Dev

Passing parameters to the base class constructor

From Dev

Unsure about parameters when calling base constructor from derived class

From Dev

Initialization of a member when passing parameters to a base class constructor

From Dev

How to pass base parameters from an overloaded constructor into the derived class

From Dev

How does default constructor used to instantiate object if it's super class no argument constructor has empty body?

From Dev

App crashes: class has no empty constructor

From Dev

App crashes: class has no empty constructor

From Dev

base class 'QAbstractListModel' has private copy constructor

From Dev

How do I pass constructor parameters to imported class in ECMAScript 6?

From Dev

How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

From Dev

How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

From Dev

How to access base class constructor when using CRTP

From Dev

When is a Java Implicit Constructor called compared to the Base Class Constructor?

From Dev

make sure class name exists, is public, and has an empty constructor for public fragment with class name and empty constructor

From Dev

How to avoid repeating base class constructor parameters in derived class in C++?

From Dev

What is the best way to get instance of a class from AutoFac when constructor injection has parameters known at configuration and runtime

From Dev

Empty constructor in String class

From Dev

Empty constructor in String class

From Dev

How does a constructor choose a base class constructor in C++

From Dev

How to use base constructor data to another constructor in the same class?

From Dev

How to use base constructor data to another constructor in the same class?

From Dev

How to accept parameters in a Python class constructor?

From Dev

How do I supply parameters to a constructor called by another constructor?

From Dev

Copy constructor for a class that has unique ptr of a Base class

From Dev

Why set up a constructor in a class to set a base parameter when I could do the same in a class it inherits from?

From Dev

Why set up a constructor in a class to set a base parameter when I could do the same in a class it inherits from?

From Dev

How to instantiate inherited class using base constructor?

From Dev

How to use a copy constructor with a base class?

Related Related

  1. 1

    Inheritance with base class constructor with parameters

  2. 2

    Passing parameters to the base class constructor

  3. 3

    Unsure about parameters when calling base constructor from derived class

  4. 4

    Initialization of a member when passing parameters to a base class constructor

  5. 5

    How to pass base parameters from an overloaded constructor into the derived class

  6. 6

    How does default constructor used to instantiate object if it's super class no argument constructor has empty body?

  7. 7

    App crashes: class has no empty constructor

  8. 8

    App crashes: class has no empty constructor

  9. 9

    base class 'QAbstractListModel' has private copy constructor

  10. 10

    How do I pass constructor parameters to imported class in ECMAScript 6?

  11. 11

    How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

  12. 12

    How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

  13. 13

    How to access base class constructor when using CRTP

  14. 14

    When is a Java Implicit Constructor called compared to the Base Class Constructor?

  15. 15

    make sure class name exists, is public, and has an empty constructor for public fragment with class name and empty constructor

  16. 16

    How to avoid repeating base class constructor parameters in derived class in C++?

  17. 17

    What is the best way to get instance of a class from AutoFac when constructor injection has parameters known at configuration and runtime

  18. 18

    Empty constructor in String class

  19. 19

    Empty constructor in String class

  20. 20

    How does a constructor choose a base class constructor in C++

  21. 21

    How to use base constructor data to another constructor in the same class?

  22. 22

    How to use base constructor data to another constructor in the same class?

  23. 23

    How to accept parameters in a Python class constructor?

  24. 24

    How do I supply parameters to a constructor called by another constructor?

  25. 25

    Copy constructor for a class that has unique ptr of a Base class

  26. 26

    Why set up a constructor in a class to set a base parameter when I could do the same in a class it inherits from?

  27. 27

    Why set up a constructor in a class to set a base parameter when I could do the same in a class it inherits from?

  28. 28

    How to instantiate inherited class using base constructor?

  29. 29

    How to use a copy constructor with a base class?

HotTag

Archive