在C#中使用TryParse处理数据类型时遇到麻烦 [英] Having trouble with data types using TryParse in C#

查看:54
本文介绍了在C#中使用TryParse处理数据类型时遇到麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是表单上的按钮.用户在小计文本框中输入美元金额,然后按计算按钮.然后,它计算出折扣并在表单底部的文本框中显示发票总额.我们假设如果使用小计金额输入"$",则使用解析方法将条目转换为十进制.我的问题是关于数据类型成功的问题.我现在是Bool,因为它是1或0.

The follow is a button on a form. The user enters a dollar amount in to the subtotal text box and presses the calculate button. It then figures a discount and displays the invoice total in a text box at the bottom of the form. We are suppose to use the Parsing method to convert the entry to decimal if a "$" gets entered with the subtotal amount. My question is about the data type sucess. I now it is Bool because it is a 1 or 0.

当我尝试构建表单时,出现此错误:

When I try and build the form I get this error:

错误1无法将类型'bool'隐式转换为'decimal'

Error 1 Cannot implicitly convert type 'bool' to 'decimal'

namespace InvoiceTotal
{
public partial class frmInvoiceTotal : Form
{
    public frmInvoiceTotal()
    {
        InitializeComponent();
    }

    private void btnCalculate_Click(object sender, EventArgs e)
    {
        decimal sucess = 0m;
        decimal subtotal = Decimal.TryParse(txtSubtotal.Text, out sucess);
        decimal discountPercent = .25m;
        decimal discountAmount = Math.Round(subtotal * discountPercent,2);
        decimal invoiceTotal = Math.Round(subtotal - discountAmount,2);

        txtDiscountPercent.Text = discountPercent.ToString("p1");
        txtDiscountAmount.Text = discountAmount.ToString(); // ("c");
        txtTotal.Text = invoiceTotal.ToString(); // ("c");

        txtSubtotal.Focus();
    }

我想我没有为变量成功"声明正确的数据类型?如果有人可以帮助我指出正确的方向,我将不胜感激!

I guess I am not declaring the right data type for the variable "sucess"? If someone could help point me in the right direction I would greatly appreciate!

*错误1无法将类型'bool'隐式转换为'decimal'

*Error 1 Cannot implicitly convert type 'bool' to 'decimal'

我正在Windows 8.1机器上使用Visual Studio Professional 2012.

I am using Visual Studio Professional 2012 on a Windows 8.1 machine.

推荐答案

虽然其他答案都是正确的,但我想在此基础上进行扩展.使用 TryParse 方法的原因是,在输入无效的情况下,您可以更好地控制程序流.换句话说,如果输入错误,您将要发生什么:

While the other answers are all correct, I wanted to expand upon them. The reason for the TryParse method is to allow you better control program flow in the event of invalid input. In other words what would you like to happen if the input is wrong:

private void btnCalculate_Click(object sender, EventArgs e)
{
    decimal subtotal;

    if (!decimal.TryParse(txtSubtotal.Text, out subtotal))
    {
        //Display some warning to the user
        MessageBox.Show(txtSubtotal.Text + " is not a valid number");

        //don't continue processing input
        return;
    }

    //input is good, continue processing
    decimal discountPercent = .25m;
    decimal discountAmount = Math.Round(subtotal * discountPercent,2);
    decimal invoiceTotal = Math.Round(subtotal - discountAmount,2);
}

在某些情况下,如果日期不正确,则不必控制程序流,因为无论如何您都将引发异常.在那种情况下,您只需要使用 Decimal.Parse 即可抛出 FormatException .

In some cases, it's not necessary to control the program flow in the event of bad date because you'd just throw an exception anyway. In that case you can just use Decimal.Parse which would throw a FormatException.

这篇关于在C#中使用TryParse处理数据类型时遇到麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆