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

Narisorn Yossaravas
Narisorn Yossaravas
600 Points

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

I don't understand why my Ternary If codes below won't compile. Help please...

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

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

1 Answer

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

Hi there! You have all the parts present but they're all a bit out of order. Also, the challenge is not asking you to return anything rather set the value of a variable.

So let's go through the steps of a ternary. First, we say what variable we're going to assign a value to. Then we say what expression we're basing our decision on. Finally, we give the two options with the first one being what happens if it evaluates to true and the second one if it is false. Take a look:

// textColor is the variable we're going to assign something to
// value < 0 is the expression we're checking
// if the value is less than 0 the value "red" will be assigned to textColor
// otherwise the value "green" will be assigned to textColor

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

Hope this helps! :sparkles:

Narisorn Yossaravas
Narisorn Yossaravas
600 Points

Thanks you! The code is now working.