Incorrect warning of PossibleInvalidOperationException

PossibleInvalidOperationException? I've checked the HasValue....

                MasterData cc2Stamdata = contCC2.MasterData.Where(sd => sd.EANID == eanId)                     .OrderByDescending(dt => dt.Mutation_MutationDate)                     .First();                 if (!cc2Stamdata.EACPeak.HasValue) throw new Exception("cc2Stamdata.EACPeak has no value");                 if (!cc2Stamdata.EACOffPeak.HasValue) throw new Exception("No off peak value in stamdata");                 // ReSharper disable PossibleInvalidOperationException                 long hoogOud = cc2Stamdata.EACPeak.Value;                 long laagOud = cc2Stamdata.EACOffPeak.Value;                 // ReSharper restore PossibleInvalidOperationException


We're using Resharper C# Edition Build 8.2.1000.4556

2 comments
Comment actions Permalink

I suspect it's because you're accessing a property, and the value of the property could change between invocations.

For example:

private long? eacPeak;

public long? EACPeak
{
    get
    {
        long? value = eacPeak;
        eacPeak = null;
        return eacPeak;
    }
}

...

if (EACPeak.HasValue)
{
    long value = EACPeak.Value; // InvalidOperationException here.
}

0
Comment actions Permalink

If that was always the case I could probably live with it.

But I have an example where there are four properties three of them do not get squiggles but the last one does... (A different ordering does not make a difference, only the last one gets the squiggles)

Is there no other way then to use the ugly comments?

0

Please sign in to leave a comment.