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

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
229,644 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