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

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
229,004 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
229,004 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";)