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 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
Michael Hulet
47,913 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.