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

ujz ujz
PLUS
ujz ujz
Courses Plus Student 6,664 Points

Code challenge help

In the code challenge that follows this video, we convert a regular if/else statement into a ternary if statement.

Below is my code. When I submit this, it tells me "Did you set a boolean variable to evaluate?" But that makes no sense. There is no boolean value necessary here. We are just setting textColor to red or green based on the value of int value. Why is it giving me this error?

int value = -1;
string textColor = null;

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

3 Answers

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

I was also confused, since you can also assign values in if-statements. But the thing is, that ternary if-statements just return values and dont do operations (I dont remember if he mentioned it in the video though)

It is designed for conditional assignment not for control flow.

Briain Corroon
Briain Corroon
13,689 Points

The question asked is very poorly worded and confusing. I really don't see how one could come to the correct answer without searching elsewhere. The video just doesn't provide enough information to answer the challenge. Not for the first time in this course.

And what should be wrong to search some information about tenary statements at StackOverFlow or MSDN. You should know that it is pretty common for devs to search for answers via google. This is also mentioned in almost every course.

ujz ujz
PLUS
ujz ujz
Courses Plus Student 6,664 Points

Thanks a lot Tobias. Was quite frustrated but you helped me out. Continuing down the path now... :)