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

南笙酒味 提交于 2019-12-10 12:57:59

问题


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!


回答1:


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



回答2:


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.




回答3:


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




回答4:


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.




回答5:


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.




回答6:


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



回答7:


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.




回答8:


Add this to the CharityCyclists class:

public CharityCyclists()
{
}

You can't code the line:

CharityCyclists cyclist1 = new CharityCyclists();

unless you have that constructor.




回答9:


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.




回答10:


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;
}


来源:https://stackoverflow.com/questions/13260021/why-am-i-getting-a-does-not-contain-a-constructor-that-takes-0-arguments-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!