'getting error CS0649: "field 'Program.x' is never assigned to, an will always have its default value false" even though I think it should work

I've been making a console application for c# that checks the inputted coordinates (check image) and gives you a compass like "north east" "south west" etc. type of output.

enter image description here

here is the code I have written down so far:

    public static bool north;
        public static bool south;
        public static bool west;
        public static bool east;
        static void Main(string[] args)
        {
            Console.WriteLine("Insert Coordinates:");
            int x = int.Parse(Console.ReadLine());
            int y = int.Parse(Console.ReadLine());
            if (x < 0)
            {
                bool west = true;
                bool east = false;
            }
            else if (x > 0)
            {
                bool west = false;
                bool east = true;
            }
            else if (x == 0)
            {
                bool west = false;
                bool east = false;
            }
            if (y < 0) 
            {
                bool north = true;
                bool south = false;
            }
            else if (y > 0) 
            {
                bool north = false;
                bool south = true;
            }
            else if (y == 0)
            {
                bool north = false;
                bool south = false;
            }
            if (north)
            {
                Console.WriteLine("North");
            }
            if (south)
            {
                Console.WriteLine("North");
            }
            if (east)
            {
                Console.WriteLine("North");
            }
            if (west)
            {
                Console.WriteLine("North");
            }
            if (north && east)
            {
                Console.WriteLine("North");
            }
            if (north && west)
            {
                Console.WriteLine("North");
            }
            if (south && east)
            {
                Console.WriteLine("North");
            }
            if (south && west)
            {
                Console.WriteLine("North");
            }
            else
            {
                Console.WriteLine("!");
            }
        }
    }
}

I dont know if there is a better way to do this, but most importantly I want to find out why I am getting the CS0649 error several times for each direction ("Program.north" and so on)



Solution 1:[1]

...
if (x < 0)
            {
                bool west = true;
                bool east = false;
            }
            else if (x > 0)
            {
                bool west = false;
                bool east = true;
            }
            else if (x == 0)
            {
                bool west = false;
                bool east = false;
            }
...

All these statements inside if bodies (bool <name> = <value>) are redefining the variables west east north south. If you put a type before a variable name, it will redefine that variable, and due to variable scope, their assigned value may not be available outside.

Since you are defining these variables globally, you should not redefine them:

...
if (x < 0)
            {
                west = true;
                east = false;
            }
            else if (x > 0)
            {
                west = false;
                east = true;
            }
            else if (x == 0)
            {
                west = false;
                east = false;
            }
...

By the way, it is conventional to prepend is or has for the boolean typed variable names; i.e. isEast, isWest ...etc. for convenience.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mcy