C# C# Objects Object-Oriented Programming Practice Writing Classes

Error Trying to compile

Can someone help me out?

Here is my Snapshot. https://w.trhou.se/pl0cc44gj7

2 Answers

Michael Hulet
MOD
Michael Hulet
Treehouse Moderator 46,961 Points

In Map.cs, notice that Width and Height are declared as readonly. However, in Game.cs, you seem to be trying to make a new blank Map and then assign it a width and height after it's created. Since these properties are readonly, this isn't possible, in order to get that part to compile, you need to move those assignments into the line where you create the map itself, like this:

Map map = new Map(20, 30);

int area = map.Width * map.Height;

Also, while it doesn't cause any issues yet, I foresee another problem in this code's future. The namespace you've used in some of your files, doesn't match the others. For example, it's TreehouseDefense in Game.cs, but TreeHouseDefense in Path.cs. Like most other programming languages, C# is case-sensitive, so TreehouseDefense is entirely different than TreeHouseDefense. I'd stick to TreehouseDefense in all your files because that's what's used in the videos, but as long as it's consistent, it doesn't really matter what you name it

In the Game.cs file, you need to add the values of Width and Height as parameters of the Map constructor.

Map map = new Map(8, 5);

and remove the field assignments under.