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

Thomas Matlock
Thomas Matlock
2,900 Points

Think about how to properly declare a variable and variable scope, then fix the following code so that it compiles.

I am trying figure out what needs to be moved around in order to make this code compile. This section is about variable scope and variables and I am not understanding variable scope

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

4 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
Matthew Musni
Matthew Musni
610 Points

it's better to click on the preview and see what the problem is... it seems that it has a problem in the input variable

as a result it must be like this

Program.cs
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 + ".");
            }            
        }
    }
}

i even deleted the last method at the bottom and instead put the method Console.WriteLine under if and else.

Steven Parker
Steven Parker
229,744 Points

The challenge instructions say "Be sure to not change the intent or intended behavior of the code.". While this modification does exhibit the same behavior, it alters the intent of providing an opportunity to properly scope the output variable.

Matthew Musni
Matthew Musni
610 Points

ohh can I see what the code should like. I got no ideas. thanks!

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {            
            string input = Console.ReadLine();
            string output = input == "quit" ? "Goodbye":"You entered " + input + ".";
            Console.WriteLine(output);
        }
    }
}