0

I get this error: This exception was originally thrown at this call stack: Can anyone help me? I tried replacing the Convert.ToDouble with Double.Parse I've defined the price_salad and the other ones but I cant get it to work? This is on c#

[External Code]
menu1.frm_item.button4_Click(object, System.EventArgs) in items.cs
[External Code]
menu1.Program.Main() in Program.cs . 

Can anyone help me? I tried replacing the Convert.ToDouble with Double.Parse I've defined the price_salad and the other ones but I cant get it to work? This is on c#

private void button4_Click(object sender, EventArgs e)
{
    double[] itemcost = new double[13];`
    itemcost[0] = Convert.ToDouble(textBox5.Text) * price_salad1;
    itemcost[1] = Convert.ToDouble(textBox6.Text) * price_salad2;
    itemcost[2] = Convert.ToDouble(textBox7.Text) * price_salad3;
    itemcost[3] = Convert.ToDouble(textBox8.Text) * price_pizza1;
    itemcost[4] = Convert.ToDouble(textBox10.Text) * price_pizza2;
    itemcost[5] = Convert.ToDouble(textBox9.Text) * price_pizza3;
    itemcost[6] = Convert.ToDouble(textBox11.Text) * price_dessert1;
    itemcost[7] = Convert.ToDouble(textBox13.Text) * price_dessert2;
    itemcost[8] = Convert.ToDouble(textBox12.Text) * price_dessert3;
    itemcost[9] = Convert.ToDouble(textBox14.Text) * price_drinks1;
    itemcost[10] = Convert.ToDouble(textBox16.Text) * price_drinks2;
    itemcost[11] = Convert.ToDouble(textBox15.Text) * price_drinks3;
}
Vizel Leonid
  • 456
  • 7
  • 15
Sleepy
  • 9
  • 1
  • 2
    You can use `double.TryParse()` to test if the conversion will work. – Crowcoder Jan 11 '21 at 16:01
  • Unrelated: Do not use floating point types for monetary amounts. – Fildor Jan 11 '21 at 16:03
  • 3
    Who teach all of you to convert data this way? It's till the first wrong entered value. Problem is that you can enter something like 0 .1 in a text box and it ofcourse will crash convertion. I'm sure that you just mistyped some value. – Ivan Khorin Jan 11 '21 at 16:04
  • Convert.ToDouble does not like empty values. The error you are getting is likely because the textboxes are empty. You probably want to set them to "0". – NTDLS Jan 11 '21 at 16:04
  • 2
    Why do you go with Double in the first place? Is anyone ordering 1½ pizza with 2.8 drinks? – Palle Due Jan 11 '21 at 16:40
  • thank you everyone i just set the value in the textbox to 0 and it worked. – Sleepy Jan 12 '21 at 07:56

1 Answers1

1

I am guessing the format exception is being cause by the Textboxes containing invalid or empty values. To fix this you need to ensure that the TextBox values are valid before using them in the calculations. I will demonstrate this using an in-line IF statement.

Example:

double[] itemcost = new double[13];` //You shouldn't use double for storing cost
int qty=0;
itemcost[0] = ( Int32.TryParse(textBox5.Text, out qty) )? qty * price_salad1: 0d;

The Int32.TryParse method attempts to convert a string representation of a number to an integer. If it returns true, the conversion was successful, otherwise it will return false. The code snippet uses an inline IF statement to first check to see if the conversion worked. If so, it performs the calculation, else it returns 0;

References you should check out:

Int32.TryParse() Function

? operator (In-line IF)

Best data type for storing money values

Decimal Data Type

Mentat
  • 356
  • 1
  • 5