问题描述
decimal DisCp = Convert.ToDecimal(txtDiscountedCashPrice.Text.Trim())* Convert.ToInt16(txtQuantity.Text.Trim());
我尝试过:
decimal DisCp = Convert.ToDecimal(txtDiscountedCashPrice.Text.Trim())* Convert.ToInt16(txtQuantity.Text。 Trim());
decimal DisCp = Convert.ToDecimal(txtDiscountedCashPrice.Text.Trim()) * Convert.ToInt16(txtQuantity.Text.Trim());
What I have tried:
decimal DisCp = Convert.ToDecimal(txtDiscountedCashPrice.Text.Trim()) * Convert.ToInt16(txtQuantity.Text.Trim());
推荐答案
decimal discountedCashPrice, quantity;
if (!decimal.TryParse(txtDiscountedCashPrice.Text.Trim(), out discountedCashPrice))
{
... report problem to user
return;
}
if (!decimal.TryParse(txtQuantity.Text.Trim(), out quantity))
{
... report problem to user
return;
}
decimal DisCp = discountedCashPrice * quantity;
如果用户输入错误,转换方法总是抛出异常 - 他们这样做,用户,所有的时间 - 但TryParse方法没有 - 他们允许你优雅地告诉用户问题是什么,并让他修复它而不是崩溃你的应用程序。
The Convert methods always throw an exception if the user enters something wrong - and they do that, users, all the time - but the TryParse methods don't - they allow you to gracefully tell the user what the problem is, and let him fix it instead of crashing your app.
这篇关于请帮我修复此错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!