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 trialDaniel W
1,341 PointsWhy is the private setter removed in the Location property? How would you set a property if it's a computed property?
So we just changed
//Code example 0
MapLocation Location{get; private set;}
into the following:
//Code example 1
public MapLocation Location {
get {
return _path.GetLocationAt(_pathStep);
}
}
So far, we can add a setter to this extended version of the code if we want like so?
//Code example 2
public MapLocation Location {
get {
return _path.GetLocationAt(_pathStep);
}
private set; //Does this work?
}
After the code example 1 we could rewrite it further into this: ?
//Code example 3
public MapLocation Location => _path.GetLocationAt(_pathStep);
//How can we have a setter now? Do we just make another method? The setter is default private here?
What I don't understand, is why we removed the private setter at all? What if we wanted to set the MapLocation in a method? Is it possible with the short-hand sugar code example 3
The remove of the setter is done minute 02:22 in The following video
1 Answer
Steven Parker
231,198 PointsA computed property has no setter at all, not even a private one.
Since the value is computed, there's no associated storage that might be assigned by a setter. If you added a setter anyway (the compiler won't stop you), you would be creating a storage "black hole" where you could put something but never get it back.
Matthew McGuff
4,355 PointsMatthew McGuff
4,355 PointsRemember that _pathStep is private. That means that it can only be modified in the class it was instantiated and in this case this is the Invader class. Whether we the setter is public or private is really important as the variable _pathStep being private prevents anyone from setting it anyway. Therefore the omission of "private set" does affect the operation of the code.
Jeremy could have point that out better as it was confusing to me to till I looked at more closely.