Danny
Danny

Reputation: 601

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!

Upvotes: 7

Views: 7729

Answers (10)

JG in SD
JG in SD

Reputation: 5607

If you declare a constructor classes do not automatically get a default constructor. You will need to create a parameterless constructor to resolve the issue, or call the one that accepts the parameters.

Upvotes: 1

Jules
Jules

Reputation: 91

You don't have a constructor for that class that has no arguments. The only constructor you have takes parameters. Add this to your class:

public CharityCyclists() { } 

Upvotes: 1

Zack
Zack

Reputation: 2869

When you provided a constructor for your class that takes arguments, the compiler no longer creates an empty constructor.

Therefore, you cannot call an empty constructor because it does not exist. You would need to explicitly write the constructor that takes 0 arguments in your class's code.

Upvotes: 2

A B
A B

Reputation: 4148

You're trying to instantiate cyclist1 an instance of CharityCyclists class without any arguments - that requires a constructor with no arguments.

But the definition of the CharityCyclists class only has one constructor with 9 arguments. Since that constructor requires 9 arguments, cyclist1 will not match that constructor. You need a constructor that takes no arguments as in:

public CharityCyclists()
{
    this.bicycle ="";
    this.wheels = 0;       
    this.FundsRaised = 0.0;
}

Upvotes: 0

Babak Naffas
Babak Naffas

Reputation: 12561

In .NET, a no args (parameterless) constructor implicitly exists if no other constructors have been declared. Once you declare a constructor with parameters, you must also explicitly declare another constructor with no parameters for your code to continue compiling.

Upvotes: 0

code4life
code4life

Reputation: 15794

Add this to the CharityCyclists class:

public CharityCyclists()
{
}

You can't code the line:

CharityCyclists cyclist1 = new CharityCyclists();

unless you have that constructor.

Upvotes: 0

tmesser
tmesser

Reputation: 7656

If you do not have any constructors, C# will implicitly create an empty one for you. It's functionally the same thing as you writing:

public CharityCyclists()
{
}

It only does this if you have no constructors, though. You have one, so this doesn't happen. You need to explicitly create a constructor that takes no parameters.

Upvotes: 1

Steve Danner
Steve Danner

Reputation: 22158

When you create a constructor that does not contain 0 arguments, you automatically remove the default constructor. You should create a new default constructor (with no arguments) and that will take care of the issue.

Upvotes: 1

driis
driis

Reputation: 164291

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() 
{}

Upvotes: 18

Pow-Ian
Pow-Ian

Reputation: 3635

You don't have a consturctor that takes no arguments.

you need to add

public CharityCyclists()
{
    this.bicycle = "bike";
    this.wheels = 2;
    this.FundsRaised = 0;
}

or something like that

Upvotes: 1

Related Questions