Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReSharper Possible InvalidOperationException

Tags:

c#

resharper

public class BloodPressure {
    public Int16? Diastolic { get; set; }

    private Boolean IsValid {
        get {
            var valid = false;

            if (this.Diastolic.HasValue && this.Systolic.HasValue) {
                if ((this.Diastolic.Value >= 0) && (this.Systolic.Value >= 0)) {
                    valid = true;
                }
            }

            return (valid);
        }
    }

    public Int16? Systolic { get; set; }

    public override String ToString() {
        var result = "";

        if (this.IsValid) {
            result = this.Systolic.Value.ToString("0") + "/" + this.Diastolic.Value.ToString("0");
        }
        else {
            result = null;
        }

        return (result);
    }
}

This is the line ReSharper complains about:

result = this.Systolic.Value.ToString("0") + "/" + this.Diastolic.Value.ToString("0");

Since I'm calling my validation logic beforehand I can be sure that both Systolic and Diastolic will have values I can use. Is ReSharper not seeing that, or is it complaining about something else?

Interestingly, it doesn't have a problem with this section:

if ((this.Diastolic.Value >= 0) && (this.Systolic.Value >= 0)) {
    valid = true;
}
like image 534
Yuck Avatar asked May 10 '11 15:05

Yuck


1 Answers

ReSharpers detection capabilities of stuff like this has its boundaries. ReSharper doesn't recognize that the call to this.IsValid basically is equivalent to this.Diastolic.HasValue && this.Systolic.HasValue with regards to that problem, i.e. ReSharper looks for those checks only in the same method/property.

like image 81
Daniel Hilgarth Avatar answered Oct 26 '22 23:10

Daniel Hilgarth