Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Basics (Retired) Perfect Variable Scope

ali raafat
ali raafat
444 Points

can i get help in variable scoop

i don't know why it keeps giving me an error about input

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(Exception)
            {
                  Console.WriteLine(output);
            }


        }
    }
}

1 Answer

Steven Parker
Steven Parker
215,972 Points

:point_right: Variables need to be declared before they are used (or as they are initialized).

The error you see about input is because it has not been declared.

Once you fix that, you'll also need to check that variables are declared in the outermost scope in which they are used. Remember you can assign a variable many times, but you should only declare it once.

ali raafat
ali raafat
444 Points

how can i know and how to add one iam confused

Steven Parker
Steven Parker
215,972 Points

By "one" ... do you mean declaration?

A simple declaration:
:point_right: variable_type variable_name; (example: int jellies;)

A declaration with initialization:
:point_right: variable_type variable_name = Initial_value; (example: string food = "doughnut";)