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#

Akshay Mehta
Akshay Mehta
1,831 Points

It will not let me assign a variable to a type I made through a class

This is in a file named Tower.cs:

namespace TreehouseDefense

{

class Tower

{


}

}

This is in a file name Game.cs:

namespace TreehouseDefense

{

class Game

{

    public static void Main()

    {

        Tower tower = new Tower();

    }

}

}

I keep getting an error and it says that it does not know what the type Tower is... Also, these are in the same folder.

2 Answers

Akshay Mehta
Akshay Mehta
1,831 Points

Okay, I figured it out but I'm really mad about what I did because it was a really dumb mistake... I forgot to compile all of the files at once so it couldn't find the other file due to how I was compiling. Sorry about the inconvenience.

I copied exactly what you said you have and got a compiler warning, but it still succeeded:

Game.cs(11,17): warning CS0219: The variable `tower' is assigned but its value is never used

Could you please copy and paste your exact error, to give me a better idea of what your compiler is saying?

Akshay Mehta
Akshay Mehta
1,831 Points

Yes, sorry my error was:

"Game.cs(7,13): error CS0246: The type or namespace name `Tower' could not be found. Are you missing an assembly reference? "

Game.cs:

namespace TreehouseDefense
{
    class Game
    {
        public static void Main()
        {
          Tower tower = new Tower();
        }
    }
}

Tower.cs:

namespace TreehouseDefense
{
  class Tower
  {

  }
}

You said you have these line for line? Could you try and copy-paste them from my code blocks here and see if it changes anything?