Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net System.FormatException:';输入字符串的格式不正确;转换为十进制时_Vb.net_Visual Studio - Fatal编程技术网

Vb.net System.FormatException:';输入字符串的格式不正确;转换为十进制时

Vb.net System.FormatException:';输入字符串的格式不正确;转换为十进制时,vb.net,visual-studio,Vb.net,Visual Studio,我在计算一件商品的价格 当用户选择项目时,它将显示在标签中。然后,当用户单击标签时,它将在文本框中显示总价 我以前做过,但现在出现了一个错误 System.FormatException:'输入字符串的格式不正确。' 如何更正此问题?您可以使用Decimal.TryParse()帮助验证该值是否为所需的数据类型 Dim GoodItemCost as Decimal If Decimal.TryParse(tbSpagehti.Text, GoodItemCost) then ite

我在计算一件商品的价格

当用户选择项目时,它将显示在标签中。然后,当用户单击标签时,它将在文本框中显示总价

我以前做过,但现在出现了一个错误

System.FormatException:'输入字符串的格式不正确。'


如何更正此问题?

您可以使用Decimal.TryParse()帮助验证该值是否为所需的数据类型

Dim GoodItemCost as Decimal
If Decimal.TryParse(tbSpagehti.Text, GoodItemCost) then
     itemcost(0) = GoodItemCost * Price_Spagethi
Else
     '' display an error message
End If     

例外情况很明显,输入数据未被识别为有效的
十进制值。在转换前检查每个文本框包含的内容。
Convert.ToDecimal
或更合适的
Decimal.Parse
使用用户的区域设置解析数据。如果用户输入了错误的十进制分隔符,或者输入了文本而不是数字,那么这些方法将失败。它是无法转换为十进制的空字符串。您需要测试是否有空文本框,或者确保每个未填充的框中都有一个0。对于我已创建的文本框,只能输入数字…..我将再次尝试检查它
Dim GoodItemCost as Decimal
If Decimal.TryParse(tbSpagehti.Text, GoodItemCost) then
     itemcost(0) = GoodItemCost * Price_Spagethi
Else
     '' display an error message
End If