为什么我得到一个“不包含带 0 个参数的构造函数"?错误?C#

Why am I getting a quot;does not contain a constructor that takes 0 argumentsquot; error? C#(为什么我得到一个“不包含带 0 个参数的构造函数?错误?C#)
本文介绍了为什么我得到一个“不包含带 0 个参数的构造函数"?错误?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;
    }

但是,我收到一条错误消息,提示'CycleEvent.CharityCyclists' 不包含采用 0 个参数的构造函数",它表示该错误与这部分代码有关:

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();

这是我的 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" ;
    }
}

谢谢!

推荐答案

这是因为 CharityCyclists 类没有不带参数的构造函数.

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

C# 编译器将为您生成默认构造函数,如果您没有定义其他构造函数.如果您自己定义构造函数(如您所愿),C# 编译器将不会生成默认构造函数.

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.

如果你想允许 CharityCyclists 在没有参数的情况下被构造,添加这个代码到类中:

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

public CharityCyclists() 
{}

这篇关于为什么我得到一个“不包含带 0 个参数的构造函数"?错误?C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)