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

drstrangequark
drstrangequark
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?

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

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

3 Answers

Steven Parker
Steven Parker
229,644 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,998 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";
drstrangequark
drstrangequark
8,273 Points

That did it. Thanks guys!