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 Encapsulation with Properties Property Practice

Kimmo Ojala
seal-mask
.a{fill-rule:evenodd;}techdegree
Kimmo Ojala
Python Web Development Techdegree Student 8,257 Points

Why 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
andren
28,558 Points

In 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
seal-mask
.a{fill-rule:evenodd;}techdegree
Kimmo Ojala
Python Web Development Techdegree Student 8,257 Points

Thanks 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
andren
28,558 Points

Because the Length property belongs to the Path class, it is not something that exists in the Invader class which is where the HasScored 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 called example within it you would not be able to call that simply by typing example(), you would have to reference the object like this: obj.example(). The same is true for properties.