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 trialAhmad Al-arn'uti
Courses Plus Student 684 Pointsplease help in this challenge
// the question: Think about how to properly declare a variable and variable scope, then fix the following code so that it compiles. Be sure to not change the intent or intended behavior of the code.
// the code: using System; namespace Treehouse.CodeChallenges { class Program { static void Main() {
String input;
try
{
input = Console.ReadLine();
if (input == "quit")
{
string output = "Goodbye.";
}
else
{
string output = "You entered " + input + ".";
}
}
catch (Exception)
{
Console.WriteLine(output);
throw;
}
}
}
}
// the Error: Program.cs(26,35): error CS0103: The name `output' does not exist in the current context Compilation failed: 1 error(s), 0 warnings
//please answer
2 Answers
James Churchill
Treehouse TeacherC# variables are scoped to the code block that they're declared in. What does this mean? It means that a variable can only be referenced (i.e. used) within the same code block that it's declared in. In C#, code blocks are defined using a set of curly braces {}
.
Let's look at an example.
static void Main()
{
int age = 18;
if (age >= 18)
{
// This `message` variable is only available within the `if` statement's curly braces.
string message = "You're old enough to vote!";
}
else
{
// This `message` variable is only available within the `else` statement's curly braces.
// Also, since this `message` variable is declared within a different scope
// than the `if` statement's `message` variable it's a completely different variable,
// even though it shares the same name.
string message = "You're too young to vote.";
}
// Attempting to reference the `message` variable here will cause a build error.
Console.WriteLine(message);
}
What we really want is one message
variable, not two. Furthermore, we want our message
variable to accessible outside of the if
/else
statements. We can accomplish that by declaring the message
variable outside of the if
/else
statements, like this.
static void Main()
{
int age = 18;
string message = "";
if (age >= 18)
{
// Notice the lack of a data type in front of the `message` variable.
// We're not declaring the `message` variable here,
// we're just assigning a value to the existing `message` variable.
message = "You're old enough to vote!";
}
else
{
// Same as above. We're just assigning a value here to the `message` variable.
message = "You're too young to vote.";
}
// And now this will work as expected as the `message` variable was declared
// in the same scope as this call to the `Console.WriteLine` method call.
Console.WriteLine(message);
}
I hope this helps!
~James
Samuel Savaria
4,809 PointsVariables only exist in the scope they are declared.
"Output" is currently declared in the if/else statements. That means the variable is destroyed at the next closing curly brackets. To avoid this, you need to declare your string variable in a scope that englobes both the if/else statements and the catch method.