0

I'm learning conditionals in C# and I get how they basically work.

A ? B : C

Where A = Boolean Condition

B = outcome when A == True

C = Outcome when A == False

My issue is more in writing a complex condition. I am trying to write:

(A == B || A == C) ? D : E

I would prefer a condition that looked more like:

A == (B || C) ? D : E

but that would only work when A, B, and C are Booleans and I'm trying to compare integers.

If there isn't a simple way to cut this down then that's okay. I've just been trying to work on making my code more efficient and easier to read.

Thanks

            var passedRadioGroup = (RadioGroup)sender;
            (passedRadioGroup.CheckedRadioButtonId == Resource.Id.radioButtonPass || passedRadioGroup.CheckedRadioButtonId == Resource.Id.radioButtonFail) ?
                checklistItems[passedRadioGroup.Id].PassedBool = passedRadioGroup.CheckedRadioButtonId == Resource.Id.radioButtonPass :
                checklistItems[passedRadioGroup.Id].PassedBool = null;

CheckedRadioButtonId is an int

radioButtonPass is an int

radioButtonFail is an int

PassedBool is a nullable bool

Essentially, If CheckedRadioButtonId equals either radioButtonPass or radioButtonFail, I want PassedBool to be set to true or false, otherwise I want it null.

Wesley DaBes
  • 117
  • 1
  • 7

3 Answers3

2

As madreflection stated, this doesn't do what you think it does.

Let's look at the case where
A = False
B = True
C = False

For
(A == B || A == C) ? D : E
A does not equal B but A does equal C so result is D

For
A == (B || C) ? D : E
B||C equals true. A does not equal true. So result is E.

And no, there is not a built in shortcut for this logic.

I have, however, written extensions methods in the past in code where I frequently find myself doing lots of A==X || A==Y || A == Z

public static bool In(this int val, params int[] compareTo){
    for(int i=0;i<compareTo.Length; i++){
        if(compareTo[i] == val)
            return true;
    }
    return false;
} 

Which you could then call like this

if( A.In(X,Y,Z)){  
//do stuff  
}  
Kevin
  • 7,162
  • 11
  • 46
  • 70
0

You could do it by testing for the first in an if and then test the second in an else if with a final else for the null if neither matches. (You could also do that as a switch statement with two cases and a default--but only if the Pass and Fail values are constants, which wasn't clear.)

Another way and more what you're looking for would be to chain two conditionals into one expression to mimic if-else-if-else:

var passedRadioGroup = (RadioGroup)sender;
checklistItems[passedRadioGroup.Id].PassedBool =
    (passedRadioGroup.CheckedRadioButtonId == Resource.Id.radioButtonPass
        ? true
        : (passedRadioGroup.CheckedRadioButtonId == Resource.Id.radioButtonFail
            ? false : null)
    );

That probably meets "prettier" and "more efficient" as it avoids the repeated comparison (your original compares against radioButtonPass twice); also note that ? : is an expression, so it should evaluate to a value, not execute an assignment (although you can legally do both; the value of an assignment is just the value that was assigned, so you can chain assignments like A = B = C; to set A and B both to the value of C). In your case you just want to assign the determined value to the one place, so it's more appropriate like this where the conditional is in the RValue expression of the assignment.

Rob Parker
  • 4,078
  • 1
  • 25
  • 26
0

There are many ways to do this. For example, you could also do this with Linq

int A = 2;
int[] BArr = new int[]{3,2};

var result = BArr.Contains(A);
Dan Csharpster
  • 2,662
  • 1
  • 26
  • 50