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 Loops and Final Touches Constants

Replace the magic numbers and strings in the if / else if / else statement with constants.

What's the issue with this code. Could someone respond please?

Thanks, Smita

CodeChallenge.cs
const int revenue = 125000;
const int lowRevenue = 100000;
const int mediumRevenue = 150000;
const string red = Red;
const string yellow = Yellow;
const string green = Green;

public string status = null;
if (revenue < lowRevenue)
{
    status = red;
}
else if (revenue < mediumRevenue)
{
    status = yellow;
}
else
{
    status = green;
}

3 Answers

Kristian Gausel
Kristian Gausel
14,661 Points

Your only problem seems to be here:

const string red = Red;
const string yellow = Yellow;
const string green = Green;

Which you need to do something like this for:

const string red = "red";
const string yellow = "yellow";
const string green = "green";

Also remove public from

public string status = null;
public string status = null; //Wrong
string status = null;
Noah Yasskin
Noah Yasskin
23,947 Points

my answer:

const int revenue = 125000;
const int redLine = 100000;
const int yellowLine = 150000;
string status = null;
const string codeRed = "red";
const string codeYellow = "yellow";
const string codeGreen = "green";

if (revenue < redLine)
{
    status = codeRed;
}
else if (revenue < yellowLine)
{
    status = codeYellow;
}
else
{
    status = codeGreen;
}
Elfar Oliver
Elfar Oliver
3,924 Points

const int revenue = 125000; string status = null;

const int lowRevenue = 100000; const string red = "red";

const int highRevenue = 150000; const string yellow = "yellow";

const string _else = "green";

if (revenue < lowRevenue) { status = red; } else if (revenue < highRevenue) { status = yellow; } else { status = _else; }