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# Basics (Retired) Perfect Variable Scope

Rachael Helsel
PLUS
Rachael Helsel
Courses Plus Student 490 Points

I don't understand why input is not recognized when it's in the broader scope...

I'm still getting the error that input isn't declared, but it's at the very beginning of the Main method...so I'm not clear why.

Program.cs
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);
            }
        }
    }
}
Jon Wood
Jon Wood
9,884 Points

I think input needs to be declared. Try var input = Console.ReadLine():.

1 Answer

Steven Parker
Steven Parker
231,269 Points

The variable input is only being assigned at the moment.

To be declared, it would need to be preceded by a type, or at least the keyword "var".

Also, if you move the WriteLine into the else block, you are changing the intent of the program. To preserve the original intent you'll need to make sure output is only declared once, and in the outermost scope where it used.