您好,有谁知道我如何处理system.formatexception [英] Hello, does anyone know how I can handle a system.formatexception

查看:95
本文介绍了您好,有谁知道我如何处理system.formatexception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中没有错误,至少我在错误列表中看到的错误。但每次我运行我的代码时,都会出现一个system.formatexception。我是编程和VB.NET的新手,所以我可以做些什么来解决这个问题会有所帮助。



我尝试过:



 Private Sub Button1_Click(发送者作为对象,e作为EventArgs)处理Button1.Click 
Dim Mealcost作为新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(txtHa) mburger.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。文本)

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

解方案
你写给货币格式的方法;为双打做同样的事情。



您可以在例程和任何默认/错误处理中执行Double.TryParse;返回一个有效的数字。



这样你就可以限制你需要编写的代码量。


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

解决方案

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.


这篇关于您好,有谁知道我如何处理system.formatexception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆