0

I'm new to C# and I am trying to create a constructor that uses a constructor of the base class. I need to set the values of Engine Class within Constructor of Car class.

Here's what I have now:

public Car (double brand, double model, double engineCap, double fuelCount, double fuelCap )
: base (SetEngine(engineCap, fuelCount, fuelCap))
    {
        this.brand = brand;
        this.model = model;

    }

    private static Engine SetEngine(double engineCap, double fuelCount, double fuelCap)
    {
        Engine engine = new Engine(engineCap, fuelCount, fuelCap);
        return engine;
    }
}

My implementation is probably wrong here. Here's the constructor of the base class:

  public Engine(double engineCap, double fuelCount, double fuelCap)
  {
      this.engineCap = engineCap;
      this.fuelCount = fuelCount;
      this.fuelCap = fuelCap;
  }

The base class of Car is Engine.

4
  • So what is the base class of Car? Can you post an minimal reproducible example? Commented Apr 20, 2016 at 19:14
  • @nvoigt the base class of Car is Engine. Commented Apr 20, 2016 at 19:15
  • Ok, that's somewhat weird, because a car is not an engine, a car has an engine. That would be composition instead of inheritance. But anyway, for your syntax question see my answer. Commented Apr 20, 2016 at 19:17
  • @user3653415, please do googling. the answer can be found on net. if you cant find then post it here. Commented Apr 20, 2016 at 19:19

2 Answers 2

3

Just call the base class constructor with the parameters it expects:

public Car (double brand, double model, double engineCap, double fuelCount, double fuelCap ) 
    : base (engineCap, fuelCount, fuelCap)
{
    this.brand = brand;
    this.model = model;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Create instance of Engine first and then pass it as argument to car. or create engine instance in car itself and assign to protected member of base class.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.