How can I implement this using ternary operator?
if(UnitType == null)
{
a = ElevationType
}
else
{
a = UnitType
}
Ternary operator
a = UnitType == null ? ElevationType : UnitType;
Now I want something like this
if(UnitType == null)
{
if(ElevationType == null)
{
a = StructureType
}
else{
a = ElevationType
}
}
else
{
a = UnitType
}
Can I achieve this using ternary operator? If not, what should be done?
Yes you can. A ternary conditional operator is an expression with type inferred from the type of the final two arguments. And expressions can be used as the conditional in an if statement.
In the above syntax, we have tested 2 conditions in a single statement using the ternary operator. In the syntax, if condition1 is incorrect then Expression3 will be executed else if condition1 is correct then the output depends on the condition2.
The logical AND ( && ) operator (logical conjunction) for a set of boolean operands will be true if and only if all the operands are true . Otherwise it will be false .
Use the IF function, one of the logical functions, to return one value if a condition is true and another value if it's false. For example: =IF(A2>B2,"Over Budget","OK") =IF(A2=B2,B4-A4,"")
a = (UnitType == null) ? (ElevationType ?? StructureType) : UnitType;
But I stand by my comment: this is harder to understand than the if-else would be.
Or, possibly,
a = UnitType ?? ElevationType ?? StructureType;
That's reasonably clear if you're familiar with the ??
operator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With