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

Program.cs(18,31): error CS0103: The name `output' does not exist in the current context. What does this mean?

I have no idea how to fix this, if someone can explain to me please.

Program.cs
using System;

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

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

            Console.WriteLine(output);

        } 
    }
}

2 Answers

S.Amir Nahravan
S.Amir Nahravan
4,008 Points

Hi @Zagreb,

If you declare your output inside if statement program can't follow it outside the if,''

change your code and declare your string output outside your if, string output ; if (input == "quit") { output = "Goodbye."; } else { output = "You entered " + input + "."; }

Luis Rodriguez
Luis Rodriguez
7,275 Points

Yes S.Amir is correct. Unless a global variable a variable will 'live' and then 'die' in the block. So you declared "string output" inside the if{} statement then once that if{} statement is done it will 'die'. Variables can in a sense only be used if they are on the same 'level'. Your "string input " is on the same level as your if and else statement. Because they are in main{}. Hope that helps my friend.