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#

Giovani Quimelli
Giovani Quimelli
6,945 Points

Why does the Length property work on the Path Object but not on the Invader one?

Why does the Length property work on the Path Object but not on the Invader one? We have to create a public "Length" field on the Path object to use when we compare "_pathStep" and "_path.Length" in the "HasScored" property of the Invader Class. Why is that? Why not just use the Length property to get the array's length just as in the Path class on the GetLocationAt method? public MapLocation GetLocationAt(int pathStep) { return (pathStep < _path.Length) ? _path[pathStep] : null; }

Steven Parker
Steven Parker
231,172 Points

Please provide a link to the course page you are working with.

Having the full code would also be helpful. You could make a snapshot of your workspace and post the link to it here.

1 Answer

Steven Parker
Steven Parker
231,172 Points

The array (and its Length) are not accessible to the Invader.

The reason you can't directly use the Length property of the internal array of the Path class is because it is a private field. That means it can only be used inside the Path class itself.

So the public Length property that is added to the Path class provides a way to get that value.