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

Andre' B.
Andre' B.
317 Points

I'm not sure what to do here? I've tried all my methods and ideas to break down the question.

I'm almost positive to solve this requires the catch(FormatException) code, but I'm not for sure if that's correct? I've tried everything I've learned and still come up short. I need help!?

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

1 Answer

You will not need to use exception handling to solve this challenge. Pay close attention to the input and output variables.

Here are a couple of hints:

  • Check the type of the input variable (what type is it?)
  • Check the scope of the output variable (can the "Console.WriteLine(output);" statement use the output variable?)

Checkout this example on scope:

{
  {
    string hello = "hi there!";
  }

  // The variable hello cannot be used here.
}

This can be fixed by doing something like this:

{
  string hello;
  {
    hello = "hi there!";
  }

  // The variable hello can now be used here.
}