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

Richard Jones
Richard Jones
3,500 Points

Did you include the boolean expression to evaluate? Yes, I did. So now what?

Not sure what I am missing. I am looking at the code in Visual Studio and it seems to work. Any ideas?

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

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

3 Answers

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

Hi there! The problem here is in the interpretation of the instructions. They are asking you to assign the value of "red" or "green" to the variable textColor. The instructions have not asked you to return anything.

The variable you should be assigning to goes on the left of the equals sign as normal. Then the expression to evaluate goes on the right side followed by a question mark. Finally the two values (the first for true and the second for false) go after the question mark and should be separated by a colon.

Here's an example:

myMood = happinessLevel < 0 ? "unhappy": "happy";

In this example if happinessLevel is less than 0, then myMood will be assigned the string "unhappy". Otherwise, it will be assigned the string "happy".

I hope this helps, but let me know if you're still stuck! :sparkles:

You only need to use a return statement if it is in a method. Try this code instead:

          code redacted by moderator
Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi Hugh! Explicit answers without any explanation are strongly discouraged by Treehouse. Could you please edit your answer to include an explanation? Thanks! :sparkles:

Steven Parker
Steven Parker
231,236 Points

:information_source: When formatting code, the three marks that start and end the coding area are backticks (```), not apostrophes ('''').

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

I have redacted the explicit answer posted above as an explanation for how/why the code works the way it does was never provided by the original poster of the answer.

Richard Jones
Richard Jones
3,500 Points

Thanks, I have been at this too long. Why was I returning anything. Taking a break.

It takes Practice, and an unbending will.