C# 类型'的例外情况;System.FormatException';关于double.Parse

C# 类型'的例外情况;System.FormatException';关于double.Parse,c#,exception,C#,Exception,mscorlib.dll中发生类型为“System.FormatException”的第一次意外异常 其他信息:输入字符串的格式不正确 之后立即跳到此代码 double parts = double.Parse(partsTextBox.Text); 文本框上没有任何内容我应该在应用程序中输入一个数字 我尝试了Try.Parse,得到了一个错误“方法'TryParse'不重载1个参数” 我不知道那是什么意思 这就是全部代码 namespace Project03_18Mar15_Alan_Me

mscorlib.dll中发生类型为“System.FormatException”的第一次意外异常

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

之后立即跳到此代码

double parts = double.Parse(partsTextBox.Text);
文本框上没有任何内容我应该在应用程序中输入一个数字

我尝试了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();
    }

}
}

消息“方法'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");
}
编辑2:

请注意,在您的代码中,有两个解析器:

        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!");
        }

你看过文件了吗?还有一个例子。发生这种情况时,文本框中是什么?什么都没有?TryParse带了一个字符串和一个out参数。创建一个double对象并将其作为out参数“double result=0”传入;如果(double.TryParse(partsTextBox.Text,out result)){},那么您的问题是如何避免异常或如何使用
TryParse
?如何避免异常,我只想能够使用我创建的表单,而不显示此消息,也不使我的应用程序崩溃。我编辑了答案并添加了如何处理异常。如果有帮助,我可以发送整个代码,并查看是否有任何地方我弄乱了。无法通过此操作,整个代码将更易于查看。可能这部分代码中没有抛出异常。请参阅Double.TryParse文档的这一部分“--------------------------------------------------------------此重载不同于Double.Parse(字符串)方法返回一个布尔值,该值指示解析操作是否成功,而不是返回解析后的数值。这样,在s无效且无法成功解析的情况下,无需使用异常处理来测试FormatException。”