Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "ref" keyword required in Double.TryParse

Tags:

c#

out

ref

I have a library with the following code:

double result = -1;

if (!string.IsNullOrEmpty(fieldName))
{
    string value = HttpContext.Current.Request.QueryString[fieldName];
    if (string.IsNullOrEmpty(value))
    {
        value = HttpContext.Current.Request.Form[fieldName];
    }

    if (string.IsNullOrWhiteSpace(value) || !double.TryParse(value.Trim(), out result))
    {
        result = -1;
    }
}

return result;

In my local copy of Visual Studio 2015 this compiles fine. Last week the build server would let this compile without a problem as well. Suddenly, on Monday the build server started having the error:

QueryParser.cs(449,53): error CS1620: Argument 2 must be passed with the 'ref' keyword [$projectPath\Core40.csproj]

When I make that switch (using ref instead of out), the build server is able to complete the build without errors, but Visual Studio will no longer compile.

From my reading of the relevant documentation, double.TryParse has always taken the out keyword, so there must be something off about the build server's libraries, but I'm not sure what or how to go about diagnosing it.


After some of the questions below, I went back to the class and confirmed that there are multiple instances of decimal.TryParse(value, out result), int.TryParse(value, out result), float.TryParse(value, out result), long.TryParse(value, out result), and Guid.TryParse(value, out result). I also was able to replace "double" with "float", and it worked just fine. This is something specific to double.


Apparently, the build command is:

msbuild  /m /p:Configuration=Release /p:Platform="Any CPU" $path\$solution.sln

Also, I can replace the TryParse with a try { return double.Parse(value); } catch (Exception) { return DEFAUlT; } block (my temporary way forward), but that doesn't actually solve the problem.

like image 920
cwallenpoole Avatar asked Aug 27 '16 02:08

cwallenpoole


1 Answers

Turns out that this was some type of bad cache issue. Eventually, DevOps rebooted the build machine and the problem went away. I'd love to have an actual root cause, but the resolution was still "turning off and turning it on again."

like image 120
cwallenpoole Avatar answered Oct 06 '22 12:10

cwallenpoole