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

John Krueger
John Krueger
1,996 Points

Boolean expression?

Keeps saying that I need to include a boolean operator. But I thought the "?" was a bool op. what am I missing?

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

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

2 Answers

Hi John,

You don't need the return statement because you're not trying to return a value from a method.

I believe what you have left should be passing the challenge but it's not.

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

The challenge might be trying to get you to follow a better practice. Notice that you're repeating the textColor variable here.

You could have the textColor on the left and then only "red" and "green" in the ternary statement.

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

The ternary statement will either evaluate to "red" or "green" and that result will then be assigned to textColor

This way you're avoiding repetition.

Feisty Mullah
Feisty Mullah
25,849 Points

int value = -1; string textColor = null;

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

your code should look like this:

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

hope it helps.

You've taken out the declaration of the textColor variable so it's not going to pass like that.