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

8,273 Points

Not sure what I'm doing wrong here

I've changed the if/else statement to a ternary if statement. The format looks just like it does in the lesson but it's still giving me an error and asking if I included the boolean expression to evaluate. I thought that the boolean expression was whether "value" is less than 0.

Can anyone point me to what I'm doing wrong here?

int value = -1;
string textColor = null;

public bool ColorChoice(int value)
    return (value < 0) ? textColor = "red" : textColor = "green";

3 Answers

Steven Parker
Steven Parker
215,939 Points

This challenge doesn't involve creating a function. But if it did, you'd still need to call the function to make something happen.

And the entire ternary is an expression. 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;
Raffael Dettling
Raffael Dettling
32,997 Points

You dont need the function^^ Use te tenary if statment to assign a value to the textColor variable :) And in an tenary statment you dont have variable names just the values it can assign

textColor = (value < 0) ?   "red" : "green";
8,273 Points

That did it. Thanks guys!