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

variable scope

The question is Think about how to properly declare a variable and variable scope, then fix the following code so that it compiles. Be sure to not change the intent or intended behavior of the code.

I am completely stumped, I do know that the problem is with the input not being in the right place but cannot figure out where it is supposed to be

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

3 Answers

Steven Parker
Steven Parker
229,744 Points

Here's a few hints:

  • a variable should be declared before (or when) it is assigned
  • a variable should only be declared one time, but can be assigned any number of times
  • a variable should be declared in the outermost scope in which it is used

Hi Steven, I understand these tips but am still not getting it correct. I have tried renaming the variables to var input = Console.WriteLine etc and moving them outside the {} but it still doesn't work. Please could you assist me with the answer as I have been stuck on this one all day

Steven Parker
Steven Parker
229,744 Points

The code shown here seems to be the original challenge code, it doesn't look like you've started yet. Please show the code you're actually submitting.

Dear all I have tried different steps still couldn't solve this. Would anybody please help out with this. Thanks.

using System;

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

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

        Console.WriteLine(output);
    }
}

}

Steven Parker
Steven Parker
229,744 Points

I see you took care of declaring input, now take a look at output:

  • it is declared twice instead of once
  • it is being used by WriteLine in the main scope but declared in inner scopes

Try declaring it once in the outer scope and convert the other declarations into plain assignments.