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

Am I declaring the constants right?

Hi guys,

I replaced the values in the conditions with constants, what am I doing wrong?

Thanks a bunch mates :)

CodeChallenge.cs
const int revenue = 125000;
string status = null;

// constants values
const int red_value = 100000;
const int yellow_value = 150000;

if (revenue < red_value)
{
    const string status = "red";
}
else if (revenue < yellow_value)
{
    const string status = "yellow";
}
else
{
    const string status = "green";
}

1 Answer

Steven Parker
Steven Parker
231,275 Points

I spot two issues:

  • you replaced the numeric literals, but still need to replace the strings
  • "status" is declared at the top of the program — you can re-assign it but don't re-declare it

Thanks for the help Steven, but it still isn't working, I tried like this:

const int revenue = 125000; const string status = null;

// constants values const int red_value = 100000; const int yellow_value = 150000;

if (revenue < red_value) { status = "red"; } else if (revenue < yellow_value) { status = "yellow"; } else { status = "green"; }

Steven Parker
Steven Parker
231,275 Points

But "status" cannot be a constant because it will be re-assigned.

On the other hand, the literals like "red" can be assigned to constants.

Thanks Steven! Now I've made it ;)