-2

Resharper suggested I change this code:

if (getBeginDate)
{
    return (DateTime)RptParamsFromDate;
}

...to this:

if (getBeginDate)
{
    if (RptParamsFromDate != null) return (DateTime)RptParamsFromDate;
}

...because, "Possible 'System.InvalidOperationException'" but when I accepted the suggested change, it doesn't compile, saying, "not all code paths return a value"

The "if" block has a following "else":

else
{
    int daysToAddToToDate = DateTime.DaysInMonth(RptParamsToDate.Value.Year, RptParamsToDate.Value.Month) - 1;
    RptParamsToDate = RptParamsToDate.Value.AddDays(daysToAddToToDate);
    return (DateTime)RptParamsToDate;
}

...so I don't know why it doesn't see the "else" as making sure a value is returned.

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 1
    Both your `if` blocks need an `else` block, or there needs to be a return external to them both for all your code paths to return a value, right now you have no result if `getBeginDate` is true and `RptParamsFromDate` is null. – Preston Guillot Apr 12 '16 at 23:14
  • 1
    So, is your question about why that code could cause an InvalidOperationException, or why you're getting a build error after Resharper "fixes" it? – BJ Myers Apr 12 '16 at 23:25
  • 1
    You're getting a "possible invalid operation exception" warning because it's not safe to cast a `null` to a `DateTime`. – Preston Guillot Apr 12 '16 at 23:28
  • "not all code paths return a value" Resharper error is associated with the `if (RptParamsFromDate != null) return (DateTime)RptParamsFromDate;` . Basically it highlights that you have not set a return value when `RptParamsFromDate` is `null`. – fujiFX Apr 12 '16 at 23:29
  • @PrestonGuillot: There *is* an "else" block; it's shown above. – B. Clay Shannon-B. Crow Raven Apr 12 '16 at 23:29
  • 1
    There is one else block. There needs to be two. As I stated, you have no return path if both `getBeginDate` is true and `RptParamsFromDate` is null. It would help to illustrate this if you include a complete snippet that demonstrates your issue, as one block of code. – Preston Guillot Apr 12 '16 at 23:30
  • @BJMyers: The latter; AFAICT, the "else" block should prevent the danger of no value being returned. – B. Clay Shannon-B. Crow Raven Apr 12 '16 at 23:30
  • @B.ClayShannon The problem is that you now have *two* places where you need an else. As far as I can tell, you only handle the else for `!getBeginDate`, not for `getBeginDate && RptParamsFromDate == null` – Rob Apr 12 '16 at 23:50

3 Answers3

3

The reason you get "not all code paths return a value" is because of the if statements. Only one of your if statements returns a path if the statement is false. You need to make both statements return a value

Example:

if (getBeginDate)
{
    if (RptParamsFromDate != null)
       return (DateTime)RptParamsFromDate;
    else 
    { 
        int daysToAddToToDate = DateTime.DaysInMonth(RptParamsToDate.Value.Year, RptParamsToDate.Value.Month) - 1;
        RptParamsToDate = RptParamsToDate.Value.AddDays(daysToAddToToDate);
        return (DateTime)RptParamsToDate;
    }
}
else { return null; }
2

Okay, so your problem can be replicated with the following:

void Main()
{
    DoSomething();
}
DateTime? MaybeGetDate()
{
    return null;
}
DateTime DoSomething()
{
    DateTime? test = MaybeGetDate();
    return (DateTime)test;
}

I'm not sure what type you have for RptParamsFromDate, but it's either a Nullable<DateTime> or a reference type. In other words, the warning about a possible invalid operation exception is because you may be trying to cast null to DateTime, which is not possible.

Now, Resharper is a bit confused, because it's default 'fix' is to simply check for null. But then you end up with this:

DateTime DoSomething()
{
    DateTime? test = MaybeGetDate();
    if (test != null)
        return (DateTime)test;
}

This should be clear as to why it doesn't compile - the method doesn't return anything if test is null. So you need to manually return a suitable DateTime in this case. That's something Resharper can't or won't help with. Perhaps DateTime.MinValue is a suitable candidate in this case.

Rob
  • 26,989
  • 16
  • 82
  • 98
1

The "else" does not make sure a value is returned in your example.

The method body:

if (getBeginDate)
{
    if (RptParamsFromDate != null) return (DateTime) RptParamsFromDate;
}
else
{
    int daysToAddToToDate = DateTime.DaysInMonth(RptParamsToDate.Value.Year, RptParamsToDate.Value.Month) - 1;
    RptParamsToDate = RptParamsToDate.Value.AddDays(daysToAddToToDate);
    return (DateTime)RptParamsToDate;
}

will return nothing when getBeginDate is true AND RptParamsFromDate is null.

A returning a default value at the end of the method body can solve this problem:

if (getBeginDate)
{
    if (RptParamsFromDate != null) return (DateTime) RptParamsFromDate;
}
else
{
    int daysToAddToToDate = DateTime.DaysInMonth(RptParamsToDate.Value.Year, RptParamsToDate.Value.Month) - 1;
    RptParamsToDate = RptParamsToDate.Value.AddDays(daysToAddToToDate);
    return (DateTime)RptParamsToDate;
}

DateTime defaultValue = DateTime.MinValue;//or what have you
return defaultValue;
AGB
  • 2,230
  • 1
  • 14
  • 21