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

Chris Saddoris
Chris Saddoris
6,121 Points

Ternary If Statement Not Working

I'm trying to complete the Ternary If statement code challenge, and my line of code isn't working. After rewatching the video about them plenty of times, having it side by side, etc, I'm just confused as to where the error in my code is. I'm sure it's something I'm just blind to, and when I see it I'll facepalm, but here is my line I'm trying to enter: (value < 0) ? textColor = "red" : textColor = "green";

But it doesn't get accepted into the code challenge. What am I doing wrong?

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

if (value < 0)
{
    textColor = "red";
}
else
{
    textColor = "green";
}

1 Answer

Hannu Shemeikka
Hannu Shemeikka
16,799 Points

Hi,

You are not setting the textColor variable correctly. Ternary operator works like this:

type variableName = condition ? this gets returned if true : this gets returned if false;

You don't assign the variable inside the ternary operator. You assign the variable to the value that the ternary operator returns.

Chris Saddoris
Chris Saddoris
6,121 Points

Beautiful! Explained it perfectly. I believe the video only went over return values, so transferring that to assigning a value to a variable escaped me. Thank you so much :)