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!

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

I've only been able to fix one part of this after a week

I just need the answer so I can look at it and understand where I was confused.

using System;

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

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


1 Answer

Congratulations on finding and fixing the first issue!

There however are two issues here.

Remember, Jeremy said in the previous video that if you define a variable in a block, the variable can only be accessed inside that block. The output variable is defined inside the if block. (And also in the else block.)

You have to define this variable before the if/else code is run.

Thankfully, blocks inside of blocks can access outside blocks. So for example, if we defined a variable in the Main() method, but not in the if statement, it would be access-able in the if statement's block.

You might need this before the if condition:

string output;

Also, in the if and else blocks, you can't define the same variable again, so you remove the strings from the lines. This tells C# you want to modify a variable, and you don't want to define the same variable again.

I hope this helps. :grin:

If you don't pass after reading this and fixing your code, you may reply with your new code snippet and I'll try to help more. :smiley:

Happy C0D1NG! :tada:

:dizzy: ~Alex :dizzy:

Dude. I just about punched my computer in the face. Thank you so much!

No problem :)