C# C# Objects Encapsulation and Arrays Ternary If

Ilya Sikharulidze
PRO
Ilya Sikharulidze
Pro Student 1,071 Points

I can't seem to figure out whats wrong with my code

Use a ternary if statement instead of an if/else statement to initialize the textColor variable to the string value "red" if the value variable is less than "0", otherwise initialize the textColor variable to the string value "green". I don't understand what have I done wrong, can someone please help me

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

3 Answers

Steven Parker
Steven Parker
177,482 Points

The entire ternary expression should be on on the right side of the assignment, but it should not contain assignments. A typical usage syntax would look like this:

variable = test_expression ? true_value : false_value;
Steven Parker
Steven Parker
177,482 Points

There should only be one assignment:

textColor = (value < 0) ? "red" : "green";
Ilya Sikharulidze
PRO
Ilya Sikharulidze
Pro Student 1,071 Points

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

This code doesn't work as well, what is wrong here?

Steven Parker
Steven Parker
177,482 Points

Ilya Sikharulidze — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!