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

Fix the code so that it compiles, but don't change the intent of the code.

wtf

Program.cs
using System;

namespace Treehouse.CodeChallenges
{

  class Program
    {

        static void Main()
        {            


            if (input == "quit")
            {
                string output = "Goodbye.";

           }
            else
            {
               input = Console.ReadLine();
               string output = "You entered " + input + ".";
           Console.WriteLine(output);
           }


        }
    }
}

2 Answers

Hello Andrej,

Take a good look at the challenge, because this code changes the intent of the code in the challenge. If you click preview, you 'll notice that the compiler has trouble with finding variabels such as output and input. So the answer would be to define the variabels in static void so that they are reachable for functions and methods. And inside the if-else-statement, you should not declare the variables, but rather use them (output = "some string").

Is this helpful? Can you pass the challenge?

This was totally helpful to me! I passed the challenge, thanks for explaining it!

Jason Weakley
Jason Weakley
2,502 Points

Here's my answer:

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {            
            string input = Console.ReadLine();

            if (input == "quit")
            {
                Console.WriteLine("Goodbye.");
            }
            else
            {
                Console.WriteLine("You entered " + input + ".");
            }
        }
    }
}

Hope that helps!