Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
231,248 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
6,159 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";