-1

For example:

if (tempCalculate >= 66 && tempCalculate < 82)
            {
                int tempResult = 1;
                return tempResult;
            }

Is explicitly stating to check tempCalculate on both ends of the AND gate necessary, or is there a way to tell the code to check it on both ends without re-stating it?

Shadowtail
  • 180
  • 12
  • Yes, it may be possible, but why do you need it? What is the problem behind mentioning your variable twice? Whatever method you use, you will lose in code readability, maintainability and quality in general. – Yeldar Kurmangaliyev Sep 08 '17 at 05:25
  • 1
    You could write a method like `InRange(value, min, max)` and call that. But it's not clear at all from your question whether that's the sort of option you're looking for, nor what sort of option you _do_ want. Please explain your question better. – Peter Duniho Sep 08 '17 at 05:27
  • You could make a function like `checkBetween(tempCalculate, 66, 82)` if you want to avoid a risk of mistyping the variable name. BTW, I write this kind of code as `if (66 <= tempCalculate && tempCalculate < 82)` to make it clearer to the reader and keep the two variable names closer to perhaps make mistypes easier to see. – Ken Y-N Sep 08 '17 at 05:29
  • See marked duplicate for some examples of the _many_ different ways you could approach your stated goal. – Peter Duniho Sep 08 '17 at 05:34
  • I just asked this out if curiosity; I know that the method I described is of course the most common and most efficient. I was simply wondering if an alternative even existed – Shadowtail Sep 08 '17 at 05:41

1 Answers1

0

Currently there is no way to do this in C#. However, to satisfy the need to avoid repetition of the variable in the actual if statement, you can add a method...

if (IsBetween(tempCalculate, 66, 81))
{
    int tempResult = 1;
    return tempResult;
}
l33t
  • 18,692
  • 16
  • 103
  • 180