Reputation: 679
I am curious as to why this is giving me an error:
Input string was not in a correct format.
This error occurs because the screen is null
so it should fail the check and not cause an exception.
if (double.Parse(textDisplay.Text) >= -2147483647 & textDisplay.Text != null)
Upvotes: 0
Views: 219
Reputation: 17600
First check if it is not null. Also use double &&
as single checks both arguments. Also you are better off with double.TryParse
in case when input is in not numeric.
if (textDisplay.Text != null && double.Parse(textDisplay.Text) >= -2147483647)
Better version:
double value = 0;
if (double.TryParse(textDisplay.Text, out value) && value >= -2147483647)
Upvotes: 5
Reputation: 23107
Use TryParse
instead of Parse
and it won't raise exception and return boolean if it is valid
double res = 0;
if(double.TryParse("asd", out res))
{
var a = res;
};
Upvotes: 1
Reputation: 11309
Use TryParse
so It will not throw any error when TextBox value is empty or null
double displayValue = 0;
double.TryParse(textDisplay.Text, out displayValue)
if (displayValue >= -2147483647)
Upvotes: 0
Reputation: 113
if (double.Parse(textDisplay.Text) >= -2147483647 & textDisplay.Text != null)
You should use double '&':
if (double.Parse(textDisplay.Text) >= -2147483647 && textDisplay.Text != null)
Upvotes: 0
Reputation: 515
try && instead
if (double.Parse(textDisplay.Text) >= -2147483647 && textDisplay.Text != null)
or
double @num;
if(double.TryParse(textDisplay.Text, out @num) && @num >= -2147483647)
return @num;
Upvotes: 0