空字符串双C#
问题描述:
在这一刻,我试图从文本框中获得一个双重值:
At this moment i am trying to get a double value from textbox like this:
String.IsNullOrEmpty(textBox1.Text) ? 0.0 : Double.Parse(textBox1.Text)
但是有一个问题,我不能得到如何解析空文本框?
But there is a problem, i cant get how to parse empty textbox?
例如,如果要用OleDb和Excel与空文本框一起尝试此代码,我们将收到错误
For example if to try this code with OleDb and Excel with empty textbox, we will get error
System.FormatException:输入字符串格式不正确。
System.FormatException: Input string was not in a correct format.
答
double val;
if(!double.TryParse(textBox.Text,out val))
val = 0.0