C# 使用带有窗体的类

C# 使用带有窗体的类,c#,C#,我在第44行和第50行遇到一个错误。 它说: 方法“GetCarData”不重载1个参数 好的,我的任务是创建一个显示3个主要特性的应用程序:年份、品牌和汽车速度。年份和品牌由文本框输入,速度从0开始 有一个加速按钮,每次按下该按钮时,速度增加5;还有一个制动按钮,每次按下该按钮时,速度降低5 我无法同时使用类和窗体来显示结果。我需要在消息框中显示品牌、年份和速度。我已经在这里坐了好几个小时了,我一事无成 非常感谢您的任何帮助。我以前从未上过课 表格如下: using System; using

我在第44行和第50行遇到一个错误。 它说:

方法“GetCarData”不重载1个参数

好的,我的任务是创建一个显示3个主要特性的应用程序:年份、品牌和汽车速度。年份和品牌由文本框输入,速度从0开始

有一个加速按钮,每次按下该按钮时,速度增加5;还有一个制动按钮,每次按下该按钮时,速度降低5

我无法同时使用类和窗体来显示结果。我需要在消息框中显示品牌、年份和速度。我已经在这里坐了好几个小时了,我一事无成

非常感谢您的任何帮助。我以前从未上过课

表格如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Car_Class_BBrantley
{
    public partial class Form1 : Form
    {
        private Car myCar;

        public Form1()
        {
            myCar = new Car();

            InitializeComponent();
        }

        private void GetCarData()
        {
            try {
                myCar.Make = txtMake.Text;

                myCar.Year = int.Parse(txtModel.Text);

                myCar.Speed = 0;

            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Concat("Must enter a valid make and year model for the car. ", ex.Message, "\r\n", ex.StackTrace));
            }
        }

        private void btnAcc_Click(object sender, EventArgs e)
        {
            GetCarData();
            myCar.AccSpeed(5);
            MessageBox.Show(" Your car is a " + myCar.Year + myCar.Make + " and it is     traveling " + myCar.Speed + " mph. ");
        }

        private void btnBrake_Click(object sender, EventArgs e)
        {
            GetCarData();
            myCar.DecSpeed(5);
            MessageBox.Show(" Your car is a " + myCar.Year + myCar.Make + " and it is     traveling " + myCar.Speed + " mph. ");
        }
    }
}
如果您想查看课程:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Car_Class_BBrantley
{
    class Car
    {
        private int year;
        private string make;
        private int speed;

        public Car()
        {
            this.year = 1994;
            this.make = "Ford";
            this.speed = 0;
        }

        public Car(string make, int year, int speed)
        {
            this.year = year;
            this.make = make;
            this.speed = speed;
        }

        public string Make
        {
            get { return make; }
            set { make = value; }
        }

        public int Year
        {
            get { return Year; }
            set { Year = value; }
        }

        public int Speed
        {
            get { return speed; }
            set { speed = value; }
        }

        public void AccSpeed(int speedIncrement)
        {
            //Add check for speed limit ranges
            Speed += speedIncrement;
        }

        public void DecSpeed(int speedDecrement)
        {
            //Add check for speed limit ranges
            Speed -= speedDecrement;
        }
    }
}
如果错误显示

方法“GetCarData”不重载1个参数

然后查看您对
GetCarData
的调用,并检查它们是否未尝试向其传递参数(参数)。它被宣布为

private void GetCarData()
这很好,两个调用(第44行和第50行)与此一致:

GetCarData();

因此,请确保计算机上的代码与此处的代码匹配,保存并重新编译。

保存并重新编译。消息说,
'Car\u Class\u BBrantley.Form1.myCar()'必须声明一个主体
,但您不再拥有
myCar()
,只有
myCar
。您的代码看起来很好。这两个类在同一个程序集中吗?如果不是这样,
Car
应该是公共的,但是在这种情况下,您会得到不同的错误。不确定这会有什么不同,但是您应该
new
InitializeComponent()之后安装
Car
。当你把事情放在前面时,Winforms会生气。你的
Year
属性声明不正确(它是递归的)。将您在属性中设置和获取的值小写(例如,
返回年份
=>
返回年份
)。@PoweredByOrange我这里的旁注比空白处的旁注多;)