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 is wrong with this: int value = -1; string textColor = null; textColor = (value < 0 ) ? "red" : "green";

I keep getting the error Did you include the condition?

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

For the last line used the following reference: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator

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

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

2 Answers

Steven Parker
Steven Parker
231,268 Points

Your solution is technically correct, and would work fine in a practical application. :+1:

However, the challenge validation system seems to be confused by the extra space inside the parentheses. So just write it this way to pass the challenge:

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

You could also omit the parentheses entirely.

Steven, thank you for the time you have taken to look at this. (And apologies for the duplicate question! I have not used this type of discussion before and my mistakes were immediately apparent as I progressedwith the question...) After posting the question and logging out I realised that the challenge validation system probably didn't like the commented outcode. The next time I logged in, I deleted the comments and it worked fine. I can't remember whether I also deleted the extra space! I shall keep this in mind in future.

Steven Parker
Steven Parker
231,268 Points

Mary Hough — Without the space it passes even with the comments left in.

Glad I could help. You can mark a question solved by choosing a "best answer".
And happy coding!