Click here to Skip to main content
15,888,221 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have no errors in my code, at least from what I can see on the error list. but every time I run my code it comes up with a system.formatexception. am new to coding and VB.NET so any advise on what I can do to solve this problem would help.

What I have tried:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim Mealcost As New Cmeal
    Mealcost.Tea = Mealcost.pTea * Double.Parse(txtTea.Text)
    Mealcost.Soft_Drink = Mealcost.pSoft_Drink * Double.Parse(txtSoft_Drink.text)
    Mealcost.Coffee = Mealcost.pCoffee * Double.Parse(txtCoffee.text)
    Mealcost.Fruit_Juice = Mealcost.pFruit_Juice * Double.Parse(txtFruit_Juice.text)
    Mealcost.Water = Mealcost.pWater * Double.Parse(txtWater.text)


    Mealcost.Cake = Mealcost.pCake * Double.Parse(txtCake.text)
    Mealcost.Muffin = Mealcost.pMuffin * Double.Parse(txtMuffin.text)
    Mealcost.Waffles = Mealcost.pWaffles * Double.Parse(txtWaffles.Text)



    Mealcost.Hamburger = Mealcost.pHamburger * Double.Parse(txtHamburger.text)
    Mealcost.Fish_Chips = Mealcost.pFish_Chips * Double.Parse(txtFish_Chips.text)
    Mealcost.Meat_Pap = Mealcost.pMeat_Pap * Double.Parse(txtMeat_Pap.text)
    Mealcost.Bunny_Chow = Mealcost.pBunny_Chow * Double.Parse(txtBunny_Chow.text)
    Mealcost.Isitambu = Mealcost.pIsitambu * Double.Parse(txtIsitambu.Text)


    Mealcost.Greek_Salad = Mealcost.pGreek_Salad * Double.Parse(txtGreek_Salad.text)
    Mealcost.Potatoe_Salad = Mealcost.pPotatoe_Salad * Double.Parse(txtPotatoe_Salad.text)
    Mealcost.Fruit_Salad = Mealcost.pFruit_Salad * Double.Parse(txtFruit_Salad.Text)

    Dim iSubtotal As Double = Mealcost.GetAmount
    Dim iVat As Double = Mealcost.GetVat
    Dim iTotal As Double = iSubtotal + iVat

    vat.Text = FormatCurrency(iVat)
    sub_total.Text = FormatCurrency(iSubtotal)
    total.Text = FormatCurrency(iTotal)


End Sub
Posted
Updated 21-Apr-19 7:47am
Comments
F-ES Sitecore 21-Apr-19 13:38pm    
Use Double.TryParse which will allow you to gracefully handle any format errors.

1 solution

You wrote a method to format Currency; do the same for doubles.

You can do the Double.TryParse in the routine and any default / error handling; returning a valid number.

That way you limit the amount of code you need to write.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900