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

I'm stuck with this one, can anybody help me? try / catch

Review the challenge thank you.

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

2 Answers

Steven Parker
Steven Parker
229,786 Points

Here are a few hints:

  • This challenge is about variable scope, it does not involve try and catch
  • A variable should be defined in the outermost scope it is used in.
  • A variable should be defined before it is assigned (or at the same time).
  • A variable can be assigned many times, but should only be defined once.

It's more simple then we think it is. It took me so long to figure this one out, and i thought 'Steven Parker' was wrong, but yeah he is pretty much right.

Pay attention to his tip about where to assign the variable and read what the console error tells you. I was pretty much redefining variables multiple times.