Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.