繁体   English   中英

麻烦添加计算变量c#

[英]trouble adding calculated variables c#

基本上,我需要将resulta + resultb + resultc +先前定义的propertyPrice一起添加到总计中,然后在文本框中显示总计。 Resulta / b / c基于propertyPrice *计算,一个常数,属性价格由用户输入。

我没有try-catch就做了,并且得到了格式异常。

int propertyPrice;

if (Int32.TryParse(propertyPriceTextBox.Text, out propertyPrice))
{
    stateSalesTaxTextBox.Text = (stateSalesTax * propertyPrice).ToString("c");

    if (residentialRadioButton.Checked == true)
        comissionTextBox.Text = (residentialCom * propertyPrice).ToString("c");

    if (commercialRadioButton.Checked == true)
        comissionTextBox.Text = (commercialCom * propertyPrice).ToString("c");

    if (hillsRadioButton.Checked == true)
        countySalesTaxTextBox.Text = (hilssTax * propertyPrice).ToString("c");

    if (pascoRadioButton.Checked == true)
        countySalesTaxTextBox.Text = (pascoTax * propertyPrice).ToString("c");

    if (polkRadioButton.Checked == true)
        countySalesTaxTextBox.Text = (polkTax * propertyPrice).ToString("c");

    decimal resulta;
    decimal resultb;
    decimal resultc;

    try
    {
        resulta = decimal.Parse(countySalesTaxTextBox.Text);
        resultb = decimal.Parse(stateSalesTaxTextBox.Text);
        resultc = decimal.Parse(comissionTextBox.Text);
    }
    catch (FormatException)
    {


    }

    decimal totalPrice = (resulta + resultb + resultc + propertyPrice);
    totalPriceTextBox.Text = totalPrice.ToString("c");  
}

使用decimal.TryParse ; 这将允许您检查字符串是否有效。

decimal resulta;
decimal resultb;
decimal resultc;

if (!decimal.TryParse(countySalesTaxTextBox.Text, out resulta))
{
    //take appropriate action here
}
if (!decimal.TryParse(stateSalesTaxTextBox.Text, out resultb))
{
    //take appropriate action here
}
if (!decimal.TryParse(comissionTextBox.Text, out resultc))
{
    //take appropriate action here
}

我想借此机会建议您更改变量名称:

  • resulta应该是countySalesTaxRate
  • resultb应该是stateSalesTaxRate
  • resultc应该是commissionRate
decimal resulta = 0;
decimal resultb = 0;
decimal resultc = 0;

decimal.TryParse(countySalesTaxTextBox.Text, out resulta);
decimal.TryParse(stateSalesTaxTextBox.Text, out resultb);
decimal.TryParse(comissionTextBox.Text, out resultc);

如果它不能解析该值,它将保持为0.如果TryParse成功解析,则返回true,因此如果要显示消息, TryParse == false检查TryParse == false

在您遇到文化问题之前,只需要一些额外的信息。 而且,

像这样使用Decimal.TryParse重载

Decimal.TryParse(countySalesTaxTextBox.Text, NumberStyles.Any, new CultureInfo("en-US"), out resulta);

您有几个不同的问题:

首先,发生FormatException ,因为您将非数字值传递给decimal.Parse()方法。 你需要检查你的输入。

接下来,在您的示例中,您实际上吞噬了您的异常。 它被抛出,捕获时钟捕获它,但由于你没有在块中做任何事情,你的代码只是离开catch块并继续。 因此,您的totalPrice仍然使用由于异常而未计算的变量的默认值进行计算。

正如其他人所建议的那样, decimal.TryParse()是一个更好的选择,因为它不会抛出异常。 但是,当Parse()TryParse()方法之一调用失败时,您需要决定要执行的操作。 您想假设结果为零,还是要中止计算?

尝试:

if (!decimal.TryParse(countySalesTaxTextBox.Text, out resulta)) { /*Resolve issue*/ }
if (!decimal.TryParse(stateSalesTaxTextBox.Text, out resultb)) { /*Resolve issue*/ }
if (!decimal.TryParse(comissionTextBox.Text, out resultc)) { /*Resolve issue*/ }

仔细检查我的变量名称和默认值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM