C#类方法,使用驱动+;使用英里数

C#类方法,使用驱动+;使用英里数,c#,C#,所以我应该用一个类来构建一个表单,计算行驶里程和用于找出每加仑英里数的里程数 在我的表格中,我的代码: //Create a default value of 0. double dblDefault = 0; //Create a TryParse if the input is double, if not, show error message. if (!double.TryParse(txtDriven.Text, out dblDe

所以我应该用一个类来构建一个表单,计算行驶里程和用于找出每加仑英里数的里程数

在我的表格中,我的代码:

 //Create a default value of 0.
        double dblDefault = 0;

        //Create a TryParse if the input is double, if not, show error message.
        if (!double.TryParse(txtDriven.Text, out dblDefault))
        {
            dblDefault = -1;
        }

        //Separation line...
        if (dblDefault >= -1)
        {
            double dblDriven = double.Parse(txtDriven.Text);
            double dblUsed = double.Parse(txtUsed.Text);

            CMilesPerGallon CTrans = new CMilesPerGallon();
            double dblMpgTotal = CMilesPerGallon.numofmiles(dblDriven);
            lblMpgTotal.Text = dblMpgTotal.ToString("C");
        }
        //If user inputs negative values, display message box for error.
        else
        {
            MessageBox.Show("Invalid input, must be a positive 'double' value.");
        }
public class CMilesPerGallon
{
    //Create calculation method.
    public double calculate(double numofmiles, double numofgallons)
    {
        //Acquire the math.
        double mpg = numofmiles / numofgallons;

        //Return the MPG.
        return mpg;
    }
}
在我的课堂上,我的代码:

 //Create a default value of 0.
        double dblDefault = 0;

        //Create a TryParse if the input is double, if not, show error message.
        if (!double.TryParse(txtDriven.Text, out dblDefault))
        {
            dblDefault = -1;
        }

        //Separation line...
        if (dblDefault >= -1)
        {
            double dblDriven = double.Parse(txtDriven.Text);
            double dblUsed = double.Parse(txtUsed.Text);

            CMilesPerGallon CTrans = new CMilesPerGallon();
            double dblMpgTotal = CMilesPerGallon.numofmiles(dblDriven);
            lblMpgTotal.Text = dblMpgTotal.ToString("C");
        }
        //If user inputs negative values, display message box for error.
        else
        {
            MessageBox.Show("Invalid input, must be a positive 'double' value.");
        }
public class CMilesPerGallon
{
    //Create calculation method.
    public double calculate(double numofmiles, double numofgallons)
    {
        //Acquire the math.
        double mpg = numofmiles / numofgallons;

        //Return the MPG.
        return mpg;
    }
}

我知道在那里的某个地方我做错了什么,但我似乎不明白。到目前为止,我得到的唯一错误是“double dblMpgTotal=CMilesPerGallon.numomiles(dblDriven)”。因为“numfmiles”不包含定义。

CMilesPerGallon
类中没有名为
numfmiles
的方法,但是,我假设您希望将
dblDriven
dblUsed
作为
calculate
方法的参数传递

e、 g


请注意,
calculate
方法在
CTrans
实例上运行,而不是通过类
CMilesPerGallon
直接调用,因为它不是静态的。

您对变量范围有误解。调用上下文中不存在变量
numofmiles
numofallons
,调用上下文使用
dblDriven
dblUsed
。因此,它应该是:

double dblDriven = double.Parse(txtDriven.Text);
double dblUsed = double.Parse(txtUsed.Text);

CMilesPerGallon CTrans = new CMilesPerGallon();
double dblMpgTotal = CTrans.calculate(dblDriven, dblUsed);

代码有两个问题 -用类名调用方法 -调用变量名作为方法名,该变量在该范围内也不可用

我保留了您的代码来解释问题,并对其进行了编辑,以显示应该如何调用它

在代码部分

CMilesPerGallon CTrans = new CMilesPerGallon();
double dblMpgTotal = CMilesPerGallon.numofmiles(dblDriven);
lblMpgTotal.Text = dblMpgTotal.ToString("C");
在线

double dblMpgTotal = CMilesPerGallon.numofmiles(dblDriven);
您正在使用类名调用方法
numfmiles
,而应该使用您创建的对象名

double dblMpgTotal = CTrans.numofmiles(dblDriven);
只能使用ClassName.MethodName调用静态方法。使用
cTrans
可以解决您的问题。如果您没有方法
numfmiles
,并且您想要使用
calculate
方法,那么您只需要调用

double dblMpgTotal = CTrans.calculate(dblDriven, dblUsed);

希望能有所帮助。

我明白,我不是在提供你想要的答案,但相信我,我在提供你需要的答案。在你开始并完成一个完整的程序之前,你需要一些基本的编程概念。看到你的问题,我不能给你一个直接的答案,因为确实没有。但我会给你指出正确的方向,其余的取决于你

请试着收集以下问题的答案,我很确定,当你收集以下问题的所有答案时,你会明白问题所在-

  • 定义函数/方法和执行函数/方法之间的区别
  • 将值作为参数传递给函数
  • 什么是面向对象编程
  • 类和对象之间的区别,类方法和对象方法之间的类似区别

还有更多,但让我们从这些开始。

CMilesPerGallon
没有名为
numfmiles()的方法。
。您的建议仍将导致编译器错误。我建议你给出一个完整的答案,显示正确的调用方法。@Code学徒我希望我更新的答案能够完成你的评论。你是想调用计算方法还是有一个numfmiles方法你没有显示出来?这是正确的。所以我把它转了一圈,做了这样一件事,“double dblMpgTotal=CMilesPerGallon.calculate(nummomiles,nummofgallons);”,但是它说,在上下文中不存在numofmiles和numofgallons。它应该存在,因为它在我的计算方法中,在我的课堂上?或者我在这里遗漏了什么。不,参数
numofmiles
numofgallons
只能在
calculate
方法中访问。顺便说一下,任何将
取出
参数的方法,如
TryParse
都保证为其赋值。因此,为
dblDefault
指定默认值没有意义,也不需要第二次解析
txtDriven.Text
。没有名为
numfmiles()
的方法。而且,
calculate()
不是静态方法没有名为
numfmiles()
的方法。是的,我假设提出问题的人在使用某些概念时有一些误解。所以我试着用同样的代码来解释。根据你的评论,我已经编辑了它,这样他也有了最后一行,谢谢。谢谢你提供了一个更完整的答案。