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#

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

New object as parameter?

In the Treehouse Defense game there is the following method in class Map: public bool OnMap(Point point).

This method is called by the constructor of class MapLocation:

public MapLocation(int x, int y, Map map) : base(x, y) { if (!map.OnMap(this)) { throw new OutOfBoundsException(this + " is outside the boundaries of the map."); } }

I'm confused because it seems to me that the method map.OnMap would only accept a variable called point (of type Point) as its parameter. Why does it accept something else, i.e. in this case the X and Y values of a variable of type MapLocation that is being constructed?

Also, the method public bool OnMap(Point point) seems to indicate to me that a variable called point is declared. Is this not an object? if it is an object, how can it be replaced by the X and Y values of an object which does not exist yet?

1 Answer

Allan Clark
Allan Clark
10,810 Points

When you see the method;

public bool OnMap(Point point)

The parameter part is saying "this method accepts any object that is a point and while inside this method we will call it 'point' ." So the method does not care what the Object is called when it is passed into the method, just they type and what it will be called inside the method.

As for why the method accepts a MapLocation object, just looking at the constructor you provided

public MapLocation(int x, int y, Map map) : base(x, y)

where it says 'base(x,y)' is calling the constructor of the parent class of this object. While I haven't looked at the code I would venture to guess that the base class here is of type Point. This will be noted at the top next to the name of the class should look like this:

public class MapLocation : Point

Since MapLocation extends Point it is a point so it can be passed in anywhere a Point is expected.

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

Thanks Allan!

I suspected something like this, but this issue puzzled so much that it's good to this question answered. It's good to know that the method doesn't care what the object is called, as long as it's of the right type. I also appreciated your explanation the while inside the method the object is called ´point´.

Thanks a bunch,

Kimmo