Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Basics (Retired) Perfect Variable Scope

Aly Khedr
Aly Khedr
470 Points

I honestly didnt quite understand the error the video was talking about. Can someone explain it?

all is said

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);
        }
    }
}

1 Answer

Steven Parker
Steven Parker
221,107 Points

There are several errors to be fixed here.

I'm not sure what part of the video you are referring to, but your question links to a challenge where instead of writing code, you fix some that has problems. Things to bear in mind:

  • variables should be declared (and optionally initialized) before they are used
  • variables should be declared only once (but may be assigned multiple times)
  • variables should be declared in the outermost scope in which they are used
  • the Preview button is helpful for showing the compiler error messages