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# Objects Object-Oriented Programming Fields

c# objects fields: CS0246 CS0841

I've followed the video twice now and bother times I get the following

Game.cs(7,13): error CS0246: The type or namespace name Map' could not be found. Are you missingTre ehouseDefence' using directive?
Game.cs(9,13): error CS0841: A local variable map' cannot be used before it is declared Game.cs(10,13): error CS0841: A local variablemap' cannot be used before it is declared
Game.cs(12,22): error CS0841: A local variable `map' cannot be used before it is declared
Compilation failed: 4 error(s), 0 warnings

I've written the code exactly the same as in the video.

Here's my code

(Game.cs)

namespace TreehouseDefense
{
    class Game
    {
        public static void Main()
        {
            Map map = new Map();

            map.Width = 8;
            map.Height = 5;

          int area = map.Width * map.Height;
        }
    }
}

(Map.cs)

namespace TreehouseDefence
{
  class Map

  {
    public int Width;
    public int Height;
  }

}

Any help would be appreciated.

1 Answer

Thank you for posting the code sample. The namepsace in each file is slightly different ("TreehouseDefense" and "TreehouseDefence"). After changing both to "TreehouseDefense", the error no longer occurs. The compiler assumed that Map was a class that still needed to be declared in your namespace.

Thank you vary much. You helped me a lot