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

Mihia Maftei
Mihia Maftei
1,315 Points

I am stock in this challenge i don't know how to fix it or how to end it either

this is the challenge asking to do "Think about how to properly declare a variable and variable scope, then fix the following code so that it compiles. Be sure to not change the intent or intended behavior of the code." If anyone done this code pls share with me to can see what i should do thx

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

2 Answers

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

input has not been declared anyway, and you declare a variable like this: *string name = * or *int age = *.

output is being declared twice but inside curly-braces ({ }) and that prevent the program from reading that variable outside the curly-braces, so you should declare it somewhere outside the curly-braces.

I would advice you to re-watch the video(s) about this subject again :-)

using System;

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

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

            Console.WriteLine(output);
        }
    }
}
Mihia Maftei
Mihia Maftei
1,315 Points

i don't know why i thought to have something to do with "try and catch" was so simple i never pass this challenge without your help thx i will be have a wee look on scope variables again ... and rthx again