Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting a "does not contain a constructor that takes 0 arguments" error? C#

On my form load, I have this code:

    private void Form1_Load(object sender, EventArgs e)
    {
        CharityCyclists cyclist1 = new CharityCyclists();
        CharityCyclists cyclist2 = new CharityCyclists("a", 1, "Finished", 0, 0, 0, "One Wheel", 1, 500);

        cyclist1.Type = "Novelty Charity Cyclist";
        cyclist1.Number = 1;
        cyclist1.Finished = "Not Finished";
        cyclist1.Hours = 0;
        cyclist1.Mins = 0;
        cyclist1.Secs = 0;
        cyclist1.Bicycle = "Tricycle";
        cyclist1.Wheels = 3;
        cyclist1.FundsRaised = 300;
    }

However, I'm getting a error saying "'CycleEvent.CharityCyclists' does not contain a constructor that takes 0 arguments", it says the error is to do with this part of the code:

CharityCyclists cyclist1 = new CharityCyclists();

Here is my CharityCyclists class:

class CharityCyclists : Cyclists
{
    private string bicycle;
    private int wheels;
    private double fundsRaised;

    public string Bicycle
    {
        get { return bicycle; }
        set { bicycle = value; }
    }

    public int Wheels
    {
        get { return wheels; }
        set { wheels = value; }
    }

    public double FundsRaised
    {
        get { return fundsRaised; }
        set { fundsRaised = value; }
    }

    public CharityCyclists(String type, int number, String finished, int hours, int mins, int secs, string bicycle, int wheels, double fundsRaised) : base(type, number, finished, hours, mins, secs, fundsRaised)
    {
        this.bicycle = bicycle;
        this.wheels = wheels;
        this.FundsRaised = fundsRaised;
    }

    public override string ToString()
    {
        return base.ToString() + " riding a " + bicycle + " with " + wheels + " wheels" ;
    }
}

Thanks!

like image 679
Danny Avatar asked Nov 06 '12 21:11

Danny


1 Answers

That is because the CharityCyclists class does not have a constructor that takes no arguments.

The C# compiler will generate the default constructor for you, if you define no other constructors. If you define a constructor yourself (as you have), the C# compiler will not generate a default constructor.

If you want to allow CharityCyclists to be constructed without parameters, add this code to the class:

public CharityCyclists() 
{}
like image 149
driis Avatar answered Oct 16 '22 00:10

driis