My problem is perhaps pretty simple, but I just started programming in C#.
My problem is, as listed above: “The type “MyProject.Bike” does not contain a constructor that takes ‘0’ arguments”.
I don’t understand, because I don’t try to make a call to this constructor with empty parentheses, and reading similar questions were all answered with “You have to much/not enough parameters…”.
I just made this for learning, but I think inheritance is a pretty important concept, so I would like to be able to do that…
My code:
using System;
namespace MijnConsoleProject
{
public class Bike
{
protected int speed = 0;
public String name
{
get;
set;
}
public void speedUp(int increment)
{
this.speed += increment;
}
public void slowDown(int decrement)
{
this.speed -= decrement;
{
public override string ToString ()
{
return name + ": speed = " + speed;
}
public Bike(int initSpeed)
{
this.speed = initSpeed;
}
}
public class GearedBike : Bike
{
private int gear;
public GearedBike(string name)
{
this.name = name;
}
public bool changeGear(int gear)
{
if(gear < 8 && gear > 0)
{
this.gear = gear;
return true;
}
else
{
return false;
}
}
public override string ToString ()
{
return name + ": speed=" + speed + ", gear=" +gear;
}
public static void main(String[] args)
{
Bike fiets = new Bike(10);
Console.WriteLine("[Normal Bike:]");
Console.WriteLine("{0}\n", fiets);
GearedBike fiets2 = new GearedBike("Fiets");
Console.WriteLine("[Geared Bike:]");
Console.WriteLine("{0}\n", fiets2);
}
}
}
Your
Bikeclass only has one constructor:This takes a single parameter.
When you derive a class that derived class’ constructor calls a constructor from the base class.
In your
GearedBikeclass’ constructor you don’t specify which constructor ofBiketo call so the compiler assumesBike(), which doesn’t exist.You probably want something like below, where we specify what base constructor to call, and pass in an appropriate value.
You might also want a
GearedBikeconstructor where you can set the speed and name, like below: