Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Objects Loops and Final Touches Foreach Loops

Amit Ghosh
Amit Ghosh
8,943 Points

Invoking InRangeOf Function

I am trying to put this into perspective, InRangeOf Functions job is to calculate the distance between the tower _location and each invader location. We are using the DistanceTo method to get the value. What I am having problem is with the implementation of that.

InRangeOf function needs a Maplocation object and a Range to check and return true or false. The if statement written here if (_location.InRangeOf(invader location, 1)). I understand this portion of it InRangeOf(invader location, 1). But how are we checking this with tower _location in the if statement. I need to clarify this so that the concepts sink into me and I can use them when required. So please be descriptive.

_location. invokes the method but _location is not a object of the tower class. What am I missing.

Thanks, Amit.

2 Answers

Steven Parker
Steven Parker
215,940 Points

_location is a MapLocation object that is part of the Tower class.

I'm not sure why you would think that is is not. It's defined as a private read-only field in the class.

When you call the InRangeOf method on _location, giving it another MapLocation object to compare to, it computes the DistanceTo that other object and returns a true/false if it is close enough.

Amit Ghosh
Amit Ghosh
8,943 Points

Thanks I get it. I was looking for the conventional class object= new obect concept to invoke the instance method. What I missed was _location is maplocation object and its invoking a maplocation class method. Instead of doing this in maplocation class we are doing this where needed i.e. tower class.

I will add this to my repertoire.