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

This code gives me an error saying that the variables are undefined, but I just don't see the problem. If you could explain it to me I'd appreciate it.

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

John Paige
John Paige
7,436 Points

Passing code below!

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

          //  Console.WriteLine(output);
        }
    }
}

You'll notice that 3 different lines were revised. Also, i commented out the last "Console.WriteLine" syntax to illustrate that it's not a necessary line of code. Hopefully this will help clarify the concepts, and that my input an be retained.

thanks I bopped my head when i saw your reply this is actually very basic i got it and im past it now

John Paige
John Paige
7,436 Points

It almost got me too. I know exactly how that was. I only figured it out because i had my workspace open alongside this challenge and i noticed the inconsistency.

John Paige
John Paige
7,436 Points

Hello Kiem Ruach. I hope my answer will help you. I'll post my passing code in a different post in case you'd prefer to just use a hint.

What the challenge objective is asking you to do is change the syntax so that it compiles. You don't actually have to change the variable scope. For example:

static void Main()
        {            
            input = Console.ReadLine();
/*On the line above, do you notice that there is a missing type?

 The word "input" needs to be set up as a string.*/
            if (input == "quit")
            {
                string output = "Goodbye.";
            }
            else  /*where you see "output", the desired function is to Write something out
 to the Console. */
            {
                string output = "You entered " + input + ".";
            }

            Console.WriteLine(output);
        }

I'll post my passing syntax in my next post.