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

Mytch Hagan
Mytch Hagan
1,876 Points

It's not working.

I tried to do as the video says and indent below the input variable, but it still says it couldn't be compiled.

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

2 Answers

Taras Metelskii
Taras Metelskii
4,167 Points

Cause you didn't define type of input variable, use string or var to declare like this string input = Console.ReadLine() and code will compile.

Mytch Hagan
Mytch Hagan
1,876 Points

It's still didn't work.

Taras Metelskii
Taras Metelskii
4,167 Points

So you have multiple declaration of output, string output need to be declared before if else statement. And in Console.WriteLine() you need to pass output variable as an argument.