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

Mohammed Gani
Mohammed Gani
501 Points

Hi Im struggling understand why my code is not compiling. I am stuck on this one .... Please assist.... Thanks

I Have used a try/catch "method" to validate input but keep getting compilation errors. I am so lost on this one please can some one assist me.

Thanks again

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {            

                try
                {
                    input = Console.ReadLine();

                        if (input == "quit")
                        {
                            string output = "Goodbye.";
                        }
                        else
                        {
                            string output = "You entered " + input + ".";
                        }
                }
                catch(FormatException)
                {
                    Console.WriteLine("This input in not valid.");
                        continue;
                }



            Console.WriteLine(output);
        }
    }
}

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hi Mohammed,

The challenge actually has nothing to do with try/catch blocks. In fact, the instructions explicitly say to "not change the intent or intended behavior of the code". When you added that block, you significantly changed both the behavior and the intent.

This challenge only deals with and wants you to correct the scope of the variables being used.

First, the input: The line input = Console.ReadLine(); is trying to initialize the variable, but it is not being declared anywhere. So, that line need to be modified, so that the variable is being declared and initialized at the same time. Without the declaration, the if/else clause has no variable to compare.

Second, the output: Again, the variable is trying to be initialized without first being declared. This one you have to watch the scope closely. The variable is having values assigned inside the if/else block, but also is being accessed outside of the block. So, if you declare it inside, it can't be accessed outside. Here, if you declare it outside and before the if/else block, it can be accessed there and also outside and after the block.

Below is the corrected code (only two small changes made). Have a look and compare with the notes above.

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {            
            string input = Console.ReadLine();  // declare the variable and assign the value (initialize).
            string output;  // declare the variable so it can be used in both scopes.

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

            Console.WriteLine(output);
        }
    }
}

I hope this helps. Keep Coding! :) :dizzy: