Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Objects Encapsulation and Arrays Ternary If

Not accepting the Ternary code

The error asks if I have included the boolean comparison in the statement. I have, what am I missing here?

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

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

2 Answers

Steven Parker
Steven Parker
215,939 Points

The termary operator takes 3 operands: the condition, the true expression and the false expression. The entire thing returns a value, but it is not a complete statement in itself, no more than "count < 8" would be.

Termaries should not be used to control execution flow. So it would not normally contain assignment statements, though it's common for it to be used in one.

A typical use of a ternary might have a structure like this:

somevariable = conditional_expression ? value_if_true : value_if_false;

I have used it nearly the exact same way as the course has (except the course puts a return prior to the start of it. I have tried it the way you are describing it (variable = condition ? true : false;) and it still errors out. The error specifically being "Did you include the "red" and "green" string literal expressions (separated by a colon) to the right of the ternary operator?".

When I don't have the variable in the front, the message says "Did you include the boolean expression to evaluate?". Which I have done so. At this point, I am not sure what they are trying to say.

Steven Parker
Steven Parker
215,939 Points

Those hints seem pretty explicit. Perhaps you should show your code again now that you've taken the assignments out of the ternary. Just guessing from the description, the one with the "variable in the front" is probably the closest to being correct.

The one about the boolean expression:

int value = -1;
string textColor = null;

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

The one with the variable in front:

int value = -1;
string textColor = (value < 0) ? textColor = "red" : textColor = "green";

I just solved it. I did it by doing what you stated, taking the assignment portion out. Things didn't click for a little bit, even after you mentioned it. However, I don't understand why the first block of code wasn't being accepted.

Steven Parker
Steven Parker
215,939 Points

The challenge may impose additional criteria beyond just successful compilation and execution.