1

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.

4
  • 3
    Decimal.TryParse returns bool not decimal. You might need to use Decimal.Parse method instead. Or assing a boolean value the result of Decimal.TryParse. Next time, please read your error messages more carefully. They are there for you. Commented Jun 1, 2014 at 17:07
  • It's always handy to tell us at which line you have the error. Commented Jun 1, 2014 at 17:08
  • 1
    " I now it is Bool because it is a 1 or 0" ??? bool in C# is true or false - not sure what you meant with "1 or 0"... Commented Jun 1, 2014 at 17:13
  • If you're using Visual Studio, then you should have squiggly lines (red or blue) under errors to show you where they are. Hover over that part of the code and the tooltip will tell you exactly which error is being flagged. If you still don't understand it, then tell us that information and we'll be better able to help you with your specific problem. Commented Jun 1, 2014 at 17:24

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Why do you invert the if statement? It's a void function and there's no need for the return.
@ThomasLindvall Because the rest of the code can't execute if the TryParse doesn't return valid data.
@ThomasLindvall - Leon Newswanger is right. The idea is to stop the rest of the method from executing. I could put all of the 'main' code within if (decimal.TryParse), but I find the way I wrote it easier to read, but the end of the day it's mostly a style issue.
Use of a return statement in this manner is pretty common in C#. It can be seen as more explicit than simply using an else statement. It tells a person reading the code that "this method stops here".
Thank you for helping me. It seems that the example I was using in the Text book is wrong. It has the code as Decimal.TryParse. When I change to lower case decimal.TryParse the error goes away. You error handling also works great!!! Once again. Thank you!!!
|
3

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

Comments

2

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.