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

Steve Agusta
Steve Agusta
6,221 Points

Problem with C# basics challenge: variable scope

I have rewritten the code to remove the output string from outside of the if statement, I understand why that was not working. But input should be able to be used inside the if statement, correct?

static void Main() {
input = Console.ReadLine();

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

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

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


        }
    }
}

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

You're correct, but there's still a problem here. Your input variable isn't declared anywhere. C# is one of those languages where you have to declare the variable before you can use it. Here's your code edited so that it passes. Take a look:

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

edited because of miscopying the code to paste into the challenge. Thought closing brace was missing

Good luck! :thumbsup:

Steve Agusta
Steve Agusta
6,221 Points

Thanks! I actually did have the closing brace, just missed it when I copied the code. I missed the string declaration in front of the input variable. I was expecting the exercise to be more related to try/catch.