我需要检查一个变量是否是数据类型double
。这是我的尝试:检查一个变量的数据类型是双重的
try
{
double price = Convert.ToDouble(txtPrice.Text);
}
catch (FormatException)
{
MessageBox.Show("Product price is not a valid price", "Product price error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
我想这会工作,但很明显,我没有意识到,如果txtPrice.Text
有比它其他一些东西,在Convert
类只会解析出来。
我该如何可靠地检查一个变量是否是双精度?
“变量”是错误的词。您正试图确定文本是否可以转换为浮点值。如果你正在确定变量的类型,你可以使用variable.GetType()== typeof(double) – Brannon 2012-03-22 14:07:54