1

So I am trying to create a program which is a hire program, I have stored 2 values as a double variable (the first one is set to the value £3.00 and the second one is set to £1.00 and I have a 2 text boxes:

  1. The first textbox is where the user will type how many hours they want.
  2. The second textbox will display the result of multiplying txtHours by costPerHour and adding £1.00 for insurance where required by the user.

The problem I am having is that it is giving the following error:

Error CS0019 Operator '*' cannot be applied to operands of type 'string' and 'double'

I have tried everything including the parse technique but it still won't work could someone please take a look at the code below and tell me where I have gone wrong?

private void button2_Click_1(object sender, EventArgs e)
    {
        double costPerHour = 3.00;
        double costOfInsurance = 1.00;

        if (chbInsurance.Checked == true)
            txtCost.Text = txtHours.Text * costPerHour + costOfInsurance;
        else
            txtCost.Text = txtHours.Text * costPerHour;
    }
Peter Campbell
  • 661
  • 1
  • 7
  • 35

5 Answers5

2

Of course the parsing will work, that's why the double.Parse() method exists. You could use it like this

txtCost.Text = (double.Parse(txtHours.Text) * costPerHour + costOfInsurance).ToString();

you're converting txtHours.Text to double, multiplying it with costPerHour and adding CostOfInsurance. After that is calculated, result is converted to string and put into txtCost control

Nino
  • 6,931
  • 2
  • 27
  • 42
1

if (chbInsurance.Checked == true) txtCost.Text = ((Convert.ToDouble(txtHours.Text.ToString())) * costPerHour + costOfInsurance).ToString();

Try this

0

You could replace

if (chbInsurance.Checked == true)
    txtCost.Text = txtHours.Text * costPerHour + costOfInsurance;
else
    txtCost.Text = txtHours.Text * costPerHour;

by

if (chbInsurance.Checked == true)
    txtCost.Text
        = (Convert.ToDouble(txtHours.Text) * Convert.ToDouble(costPerHour) + costOfInsurance)
          .ToString();
else
    txtCost.Text
        = (Convert.ToDouble(txtHours.Text) * Convert.ToDouble(costPerHour))
          .ToString();

to introduce some parsing, but this will not have meaningful error handling. An exception will be thrown if the contents of costPerHour cannot be parsed.

Codor
  • 17,447
  • 9
  • 29
  • 56
0

you can simply convert string value in int or double as per your requirement

 if (chbInsurance.Checked == true)
    txtCost.Text = (Convert.ToInt32(txtHours.Text) * costPerHour + costOfInsurance).ToString();
 else
    txtCost.Text = (Convert.ToInt32(txtHours.Text) * costPerHour).ToString(); 

What for this you must validated text box value because if it wasn't the numeric value it generate exception

Irtaza Zaidi
  • 106
  • 5
0

Using this extension methods

public static string GetSumMul(this string input, double num1, double num2)
    {
        var textBoxValue = Convert.ToDouble(input); // this might throw FormatException or OverflowException
        return ((textBoxValue*num1) + num2).ToString();
    }

    public static string GetMul(this string input, double num1)
    {
        var textBoxValue = Convert.ToDouble(input); // this might throw FormatException or OverflowException
        return (textBoxValue * num1).ToString();
    }
    private void button2_Click_1(object sender, EventArgs e)
    {
        double costPerHour = 3.00;
        double costOfInsurance = 1.00;
        try
        {
            if (chbInsurance.Checked == true)
                txtCost.Text = txtHours.Text.GetSumMul( costPerHour ,costOfInsurance);
            else
                txtCost.Text = txtHours.Text.GetMul( costPerHour);
        }
        catch (FormatException)
        {
            //"Unable to convert txtCost.Text to a Double."
        }
        catch (OverflowException)
        {
            //" txtCost.Text is outside the range of a Double."

        }

    }
Maro
  • 2,579
  • 9
  • 42
  • 79