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 trialGamingWithHan :)
Java Web Development Techdegree Student 1,966 PointsVariable Scope
Hey guys and girls, I am really stuck on this variable scope challenge cause I do not know what to do. Can someone help me?
Thanks
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
input = Console.ReadLine();
if (input == "quit")
{
string output = "Goodbye.";
}
else
{
string output = "You entered " + input + ".";
}
Console.WriteLine(output);
}
}
}
2 Answers
Antonio De Rose
20,885 Pointsusing System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
string input = Console.ReadLine();//this is correct, good work
string output;//when you declare the variable here, accessibility of this variable is high, from here
if (input == "quit")
{
output = "Goodbye.";//Now that the output is declared, you cant declare here again, so I take off the string part
}
else
{
output = "You entered " + input + ".";//here too, I take the string part, otherwise, am double doing
}
Console.WriteLine(output);
//till here, the output variable could be accessed
}
}
}
Antonio De Rose
20,885 Pointsusing System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
input = Console.ReadLine(); //variable being used without declaring, so you will need the variable type in front
if (input == "quit")
{
string output = "Goodbye.";//this is about variable scoping, string is only within this blocks
}
else
{
string output = "You entered " + input + ".";//again variable scoping, string is only available within the blocks
}
Console.WriteLine(output);// given, it was declared, in those blocks, it cannot be used here, the output
//better idea would be declare the output variable just below, where input is to be declared
//then you need not have declare the variable output, at, if and else
}
}
}
Moderator Edit: Moved response from Comments section to Answers.
GamingWithHan :)
Java Web Development Techdegree Student 1,966 PointsThanks so much Antonio!
GamingWithHan :)
Java Web Development Techdegree Student 1,966 PointsHey Antonio, Thanks for the answer! However, I am still rn having trouble with the code. Here is what i have currently:
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
string input = Console.ReadLine();
if (input == "quit")
{
string output = "Goodbye.";
}
else
{
string output = "You entered " + input + ".";
}
Console.WriteLine(output);
}
}
}
Jon Wood
9,884 PointsJon Wood
9,884 PointsAntonio De Rose that's a great explanation! I like the idea of explaining with comments in the code.