double.Parse上的类型'System.FormatException'的异常

时间:2015-03-24 20:29:26

标签: c# exception

  

类型' System.FormatException'的第一次机会异常发生在mscorlib.dll

     

其他信息:输入字符串的格式不正确。

之后立即跳转到此代码
double parts = double.Parse(partsTextBox.Text);

textBox上没有任何东西我应该在应用程序中输入一个数字

我已尝试过Try.Parse,但我收到错误"没有重载方法' TryParse'需要1个参数"

我不知道这意味着什么。

这是整个代码

namespace Project03_18Mar15_Alan_Mederos_B
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

    private void calculateTotalButton_Click(object sender, EventArgs e)
    {
        double oil = 0, lube = 0, radiator = 0, trans = 0, inspection = 0; double muffler = 0,tire = 0;
        if (oilChangeBx.Checked == true)
        {
            oil = 26;
        }
        if (lubeJobBx.Checked == true)
        {
            lube = 18;
        }
        if (radiatorFlushBx.Checked == true)
        {
            radiator = 30;
        }
        if (transFlushBx.Checked == true)
        {
            trans = 80;
        }
        if (inspectionBx.Checked == true)
        {
            inspection = 15;
        }
        if (replaceMufflerBx.Checked == true)
        {
            muffler = 100;
        }
        if (tireRotationBx.Checked == true)
        {
            tire = 20;
        }


        // Convert all values to doubles
            double parts = double.Parse(partsTextBox.Text);   
            double labor = double.Parse(laborTextBox.Text);
            double oillube = OilLubeCharges(oil, lube);
            double flush = FlushCharges(radiator, trans);
            double misc = MiscCharges(inspection, muffler, tire);
            double other = OtherCharges(parts, labor);
            double tax = TaxCharges(parts, labor, oillube, flush, misc, labor);
            double total = TotalCharges(oillube, flush, misc, other, tax);
            double services = oillube + flush + misc;
            servicesOutputLb.Text = services.ToString("c");
            partsOutputLb.Text = other.ToString("c");
            taxOutputLb.Text = tax.ToString("c");
            totalOutputLb.Text = total.ToString("c");
        }


    private double OilLubeCharges (double oil, double lube)
    {
        //return oil and lube charges
        return oil + lube;
    }
    private double FlushCharges(double radiator, double trans)
    {
    // returns radiator and transmission flush charges
        return radiator + trans;
    }
    private double MiscCharges(double inspection, double muffler, double tire)
    {
        //return inspection, muffler, and tire rotation charges
        return inspection + muffler + tire;

    }

    private double OtherCharges (double parts, double labor)
    {
        //return parts and labor
        return parts + labor;

    }

    private double TaxCharges (double parts, double labor, double oillube, 
        double flush, double misc, double other)
    { 
    //returns sales tax on parts only
        if (parts != 0 && labor != 0 && (oillube != 0
            && flush != 0 && misc != 0 && other != 0))
        {
            // sales on tax is 6%
            return (0.06 * parts);
        }
        else return 0;
    }

    private double TotalCharges(double oillube, double flush, 
        double misc, double other, double tax)
    {
        return oillube + flush + misc + other + tax;
    }

    private void clearBtn_Click(object sender, EventArgs e)
    {
        // Clears all fields
        oilChangeBx.Checked = false;
        lubeJobBx.Checked = false;
        radiatorFlushBx.Checked = false;
        transFlushBx.Checked = false;
        inspectionBx.Checked = false;
        replaceMufflerBx.Checked = false;
        tireRotationBx.Checked = false;
        partsTextBox.Text = "";
        laborTextBox.Text = "";
        servicesOutputLb.Text = "";
        partsOutputLb.Text = "";
        taxOutputLb.Text = "";
        totalOutputLb.Text = "";
    }

    private void exitBtn_Click(object sender, EventArgs e)
    {
        //exits form
        this.Close();
    }

}

}

1 个答案:

答案 0 :(得分:2)

消息“方法没有重载'TryParse'需要1个参数”是自解释的。 您只能使用两个或更多参数调用TryParse。

要使用带有两个参数的TryParse,必须将输出变量作为参考传递。请参阅以下代码:

double output;
bool success = double.TryParse(partsTextBox.Text, out output);

支付注意,输出必须通过初始化。如果成功,产出将具有价值。

其他方式是捕获异常:

double parts;

try
{ 
    parts = double.Parse(partsTextBox.Text);
}
catch (FormatException fe)
{
    MessageBox.Show("Quantity of parts not informed");
}

EDIT2:

在代码中看一下,你有两个解析器:

        double parts = double.Parse(partsTextBox.Text);   
        double labor = double.Parse(laborTextBox.Text);

他们中的任何一个都可以抛出FormatException。我的消化是这样写的:

        double parts;
        double labor;
        if(
           double.TryParse(partsTextBox.Text, out parts)
           && double.TryParse(laborTextBox.Text, out labor))
        {
            double oillube = OilLubeCharges(oil, lube);
            double flush = FlushCharges(radiator, trans);
            double misc = MiscCharges(inspection, muffler, tire);
            double other = OtherCharges(parts, labor);
            double tax = TaxCharges(parts, labor, oillube, flush, misc, labor);
            double total = TotalCharges(oillube, flush, misc, other, tax);
            double services = oillube + flush + misc;
            servicesOutputLb.Text = services.ToString("c");
            partsOutputLb.Text = other.ToString("c");
            taxOutputLb.Text = tax.ToString("c");
            totalOutputLb.Text = total.ToString("c");
        }
        else
        {
            MessageBox.Show("Quantity of parts and quantity of labors must be informed and must be valid!");
        }