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

What's wrong with this?

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

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

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

2 Answers

Alright, so firstly...I don't see a reason to return anything, since you're not in a function. The rest of it is close, but you need to set it up just a tiny bit differently. Conditional (ternary) operators begin with your boolean check, in this case

(value < 0)

This will be evaluated as either true or false, and if true it will return the expression preceding the colon, if false, it will return the expression afterward. You might look at your version

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

and think: "Wait, but that's what I did?"

For the most part, you'd be right. The issue you're having here is that the operator is basically intended to return the value yes or no for the entire expression as a question following boolean logic. In short, the the operator knows the "answer to the Ultimate Question of Life, the Universe, and Everything" and how it got that answer, but it still doesn't know what the actual question is without the context of textColor to store it in. Which is why, in this case, the "Ultimate Question" is: "textColor?" and the expected answer would be "red" or "green" depending on what mood your expression is in. Since the question already defines what you are answering, you don't need to define it as textColor = "red" or textColor = "green" and instead you wind up with something that looks like this:

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

Sorry if I lost you in there. I'm a bit tired and my sense of humor can't be helped. If I didn't explain it clearly enough, let me know and I'll try to clear it up some.

Thanks.