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

I think, my code is correct but it's not accepted. Hints don't help

What is wrong here?

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

3 Answers

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,077 Points

I believe you need to set the variable equal to the ternary operator...so try this:

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

Thank you, Dane!

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi Iryna Bondarenko!

You're doing well! It's just a little out of order. :smiley: First, we say the name of the variable whose value we're looking to change. Then we give the condition. If it evaluates to true it will change the textColor to red. If it's false, it will change the textColor to green. Take a look at the line you need:

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

This is very handy when we have a variable whose value will be set to one thing if an expression evaluates to true and another if the expression evaluates to false! Good luck! :thumbsup:

And Dane Parchment is a fast typer!

Thank you Jennifer!