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

Jorge Kalil
Jorge Kalil
1,832 Points

scope

how to resolve this ?

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,786 Points

:point_right: There are a few issues here that need fixing.

This challenge is unusual in that the provided code has errors, and your task is to fix them. The major issue here has to do with the scope of the variable named output:

  • output is defined more than once
  • output is defined inside the if statement, but it is referenced outside of it

To fix these, you'll want to move the definition of output to the proper scope, and then replace the multiple definitions with ordinary assignments.