0
votes

Assume result[11] == string.Empty (i.e. result[11] = "")

if (result[11] == string.Empty) // this block works fine
{
    user.Age = Int32.Parse(result[11]);
}
else
{
    user.Age = null;
}

// the following line will throw exception
user.Age = (result[11] == string.Empty) ? (int?) null : 
                                          Int32.Parse(result[11]);

System.FormatException was unhandled Message=Input string was not in a correct format. Source=mscorlib StackTrace: at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, >> >> NumberFormatInfo info, Boolean parseDecimal) at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at System.Int32.Parse(String s)

To me, the above two blocks are same. Then why the first one works while the second one doesn't?

5
Are you sure the first line isn't supposed to be if(result[11] != string.Empty) ? - The Evil Greebo
you should prefer string.IsNullOrEmpty( ) instead of mystring == string.Empty - Muad'Dib
The error is right in the exception message: you're parsing something that's not parseable. Perhaps the value is null, or a non-numeric string? - matt
They don't look the same. The actions are backwards - Roly
Assuming the first line is supposed to be != not ==, if string[11] is null, not empty, you cannot parse null to int, so like Maud'Dib said, use .IsNullOrEmpty() - The Evil Greebo

5 Answers

7
votes

The blocks are not the same.

if (result[11] == string.Empty) // this block works fine
{
    user.Age = Int32.Parse(result[11]);
}

That block should actually not work, because the block will only parse an empty string. Switch the code in the "if" block and the "else" block, and it will be identical to your ternary "? :" operator.

1
votes

I have tried this:

        var value = "";
        int? age;

        if (value != string.Empty)
        {
            age = Int32.Parse(value);
        }
        else
        {
            age = null;
        }


        age = (value == string.Empty) ? (int?)null : Int32.Parse(value);

and it works fine (I have changed the == to != in the first if).

1
votes

The result that you are trying to parse as Integer is not a valid Integer, hence the exceptions. Rather do the following.

if (!String.IsNullOrEmpty(result[11]))
{
    if (!Int32.TryParse(result[11], out user.Age))
        user.Age = null; // not really needed
}
1
votes

Everyone answered how you are trying to parse invalid strings as integer. They are right. However, apparently people have missed that your code is not equivalent, because you have inverted the ternary clauses. This would be your equivalent code:

//if this is your code:
if (result[11] == string.Empty) // this block works fine
{
    user.Age = Int32.Parse(result[11]);
}
else
{
    user.Age = null;
}

//This is your equivalent ternary. You have inverted here
user.Age = (result[11] == string.Empty) ? Int32.Parse(result[11]) : 
                                          null;
0
votes

result[i] might return 'object', ergo cast:

     (string) result[i] == ....
     Int.Parse(  (string) result[i] )