C# C# Objects Encapsulation and Arrays Ternary If

abdur rafay
abdur rafay
1,933 Points

I don't understand what I am doing wrong?

The formatting of the ternary if is correct, there are no compiler errors either.

CodeChallenge.cs
int value = -1;
string textColor = null;

(value < 0) ? textColor = "red" : textColor = "green";

2 Answers

Steven Parker
Steven Parker
177,727 Points

The ternary operator should be used as an expression, not as a control statement. Try coding it as a single assignment, with the entire ternary on the right side, and the true/false clauses holding only the values to be chosen. Example:

string YesNo = boolValue ? "Yes" : "No";
Victoria Holland
Victoria Holland
4,444 Points

I also got caught out by this!

I originally had value < 0 ? textColor = "red" : "green";

but it turned out I needed to swap the condition and the variable name like this: textColor = value < 0 ? "red" : "green";