我很好奇,爲什麼這是給我的錯誤:輸入字符串的不正確的格式C#
Input string was not in a correct format.
出現此錯誤是因爲屏幕是null
所以應該沒有通過檢查的,而不是造成例外。
if (double.Parse(textDisplay.Text) >= -2147483647 & textDisplay.Text != null)
我很好奇,爲什麼這是給我的錯誤:輸入字符串的不正確的格式C#
Input string was not in a correct format.
出現此錯誤是因爲屏幕是null
所以應該沒有通過檢查的,而不是造成例外。
if (double.Parse(textDisplay.Text) >= -2147483647 & textDisplay.Text != null)
首先檢查它是否不爲空。同樣使用雙重&&
作爲單個檢查兩個參數。如果輸入數字不是數字,你最好用double.TryParse
。
if (textDisplay.Text != null && double.Parse(textDisplay.Text) >= -2147483647)
更好的版本:
double value = 0;
if (double.TryParse(textDisplay.Text, out value) && value >= -2147483647)
使用TryParse
,而不是Parse
,它不會引發異常,並返回boolean值,如果它是有效的
double res = 0;
if(double.TryParse("asd", out res))
{
var a = res;
};
嘗試& &,而不是
if (double.Parse(textDisplay.Text) >= -2147483647 && textDisplay.Text != null)
或
double @num;
if(double.TryParse(textDisplay.Text, out @num) && @num >= -2147483647)
return @num;
if (double.Parse(textDisplay.Text) >= -2147483647 & textDisplay.Text != null)
您應該使用雙 '&':
if (double.Parse(textDisplay.Text) >= -2147483647 && textDisplay.Text != null)
使用TryParse
所以當文本框爲空值,否則將不會引發任何錯誤null
double displayValue = 0;
double.TryParse(textDisplay.Text, out displayValue)
if (displayValue >= -2147483647)
'textDisplay.Text'的值是什麼? –
該值爲空 –