Bob Glidewell
Bob Glidewell

Reputation: 99

Having trouble with data types using TryParse in C#

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:

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!

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

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

Upvotes: 1

Views: 1348

Answers (3)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

TryParse() returns boolean to tell that parsing was successful or not, it returns true if parsing was successful which means the value was valid decimal and returns false if it's unable to parse it, and it will output the value in the out parameter.

it should be:

decimal subtotal;
decimal invoiceTotal;
bool isDecimal= Decimal.TryParse(txtSubtotal.Text, out subtotal);

if(isDecimal)
{

    decimal discountPercent = .25m;
    decimal discountAmount = Math.Round(subtotal * discountPercent,2);
    invoiceTotal = Math.Round(subtotal - discountAmount,2);
}

According to MSDN:

Decimal.TryParse() Converts the string representation of a number to its Decimal equivalent. A return value indicates whether the conversion succeeded or failed.

See details and example on MSDN

Upvotes: 3

Philip Pittle
Philip Pittle

Reputation: 12295

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

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.

Upvotes: 3

Saverio Terracciano
Saverio Terracciano

Reputation: 3915

In this line

decimal subtotal = Decimal.TryParse(txtSubtotal.Text, out sucess);

You're assigning a boolean (the return type of TryParse http://msdn.microsoft.com/it-it/library/9zbda557(v=vs.110).aspx) to a decimal variable, that is the cause of the error.

If you want to handle the success of the operation you should do something like:

bool success = Decimal.TryParse(txtSubtotal.Text, out subtotal);

and then check the variable success to verify if the conversion was done correctly.

Upvotes: 2

Related Questions