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 trialKimmo Ojala
Python Web Development Techdegree Student 8,257 PointsWhy does if-clause in property body { return _pathStep >= _path.Length; } need another property?
// To complete the if-clause it was necessary to create another property in Path class (public int Length => _path.Length). Whereas in the below method there is no need to create neither a property nor a field to do the same calculation. Why is this?
public MapLocation GetLocationAt(int pathStep)
{
return (pathStep < _path.Length) ? _path[pathStep] : null;
}
}
}
2 Answers
andren
28,558 PointsIn the Path
class the _path
variable holds an array of MapLocation
objects. All arrays have a Length
property that tells you how many objects are in the array. Since the GetLocationAt
method is inside the Path
class it can directly reference this variable and call its Length property.
In the Invader
class the _path
variable holds a Path
instance, not an array. So even though the names of the variables are the same they actually contain completely different things. Since the MapLocation
array inside the Path
object is marked as private
it can't be accessed directly by methods outside of that class.
So to get length of that array within the Invader
class a method or property has to be created to make that info available outside the Path
class. That is the purpose of the Length
property. It simply returns the length of the _path
array, which is normally only something you can access from within the Path
class itself since it is a private variable.
Kimmo Ojala
Python Web Development Techdegree Student 8,257 PointsI see. The array _path in Path class is an object that needs to be referenced. Thanks Andren!
Kimmo Ojala
Python Web Development Techdegree Student 8,257 PointsKimmo Ojala
Python Web Development Techdegree Student 8,257 PointsThanks Andren! I had not considered that the _path variable in class Path is private. I still don't quite understand how properties are used. Why is not the name of the property (Lenght) used to get what it returns?
As in:
public bool HasScored { get { return _pathStep >= Length; } }
BR, Kimmo
andren
28,558 Pointsandren
28,558 PointsBecause the
Length
property belongs to thePath
class, it is not something that exists in theInvader
class which is where theHasScored
property is being defined. So you have to reference the_path
object to get it.If you had an object called
obj
with a method calledexample
within it you would not be able to call that simply by typingexample()
, you would have to reference the object like this:obj.example()
. The same is true for properties.