4

Below is my statement:

double? My_Value = null;
   if (Request.Form["MyValue"] != null)
    My_Value = double.Parse(Request.Form["MyValue"].ToString());

When I try to submit my form with no value for 'MyValue' I get a run time error saying 'Input string was not in a correct format.'. When I try to use:

 My_Value = double.TryParse(Request.Form["MyValue"].ToString());

Visual Studio gives me a compile error that says 'No overload for method 'TryParse' takes 1 arguments'.

When I provide a value for 'My_Value', the form submits. How do I get the program to accept a null value? Thank you.

2
  • 2
    You'll get the answer here but please take this as a starting point to learn about language rules and debugging. This is a basic question and it is essential that you develop the skills to resolve such issues immediately on your own. Commented Jun 26, 2014 at 21:28
  • As someone who might be starting, or just got stumped b/c of a brain fart, etc. suggesting to do a better google search, or learn it on your own, etc. IMHO is not that helpful, sometimes you just need a quick answer and if google to SO brings you here the discussion and answers below are much more clear than the MSDN documentation...sometimes we are switching languages and we cannot for the life of us remember how it works in C#, hence the question and answer in SO Commented Mar 18, 2019 at 15:22

4 Answers 4

4

You need to declare a double variable to store the result and pass it to TryParse as an out argument, so it will be assigned if the parse succeeded:

double result;

var isValid = double.TryParse(Request.Form["MyValue"].ToString(), out result);

Also TryParse is different than Parse method, it doesn't return a numerical result, it returns a boolean result that indicates whether the parsing is succeeded or not.So you need to assign that result and check it's value to make sure parsing was successful.Or you could directly check the result without storing it in a variable.

double result;
double? My_Value = double.TryParse(Request.Form["MyValue"].ToString(), out result)
            ? (double?)result
            : null;
Sign up to request clarification or add additional context in comments.

Comments

2

Both solutions will result in 0;

string myString = null;
double result = Convert.ToDouble(myString);

OR

string myString = null;
double result = double.Parse(myString ?? "0");

Comments

0

The problem with your first case isn't just about handling nulls but rather about handling anything that cannot be parsed (since you are accepting a value from an untrustworthy source). Using TryParse is the way to go here.

However, the TryParse method accepts two arguments, the first the value to parse, the second a value to assign to if parsing succeeds (as an out) parameter.

For just such an occasion whereby you'd like a nullable result, a custom utility method can come in handy:

public static class NullableDouble
{
  public static double? TryParse(string input)
  {
    double result;
    var success = double.TryParse(input, out result);
    return success ? result as double? : null;
  }
}

Useable like so:

var myValue = NullableDouble.TryParse((Request.Form["MyValue"] ?? string.Empty).ToString());

Comments

0

The signature for TryParse is bool TryParse(string str, out double result)

Use it like this:

double result;
var success = double.TryParse(Request.Form["MyValue"].ToString(), out result);

My_Value = success? result : 0d;

As "usr" mentioned in the comments, the next time you get an error like this, you should check the MSDN documentation first before coming here.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.