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#

Joshua Guth
Joshua Guth
1,935 Points

MapLocation parameters

Why is "map" included in the parameters for MapLocation?

andren
andren
28,558 Points

Unless you link to the video or code you are referencing it's not really possible to answer your question.

1 Answer

Ryan Watson
Ryan Watson
5,471 Points

As @andren points out, it is good practice to always include a reference to the code or video, but I will try to answer the question.

MapLocation is a child class of Point, and a point is located on the map. There are many scenarios in which the MapLocation class would need an instance of the Map class, I will use the following scenario as an example.

There is a method of the Map class called OnMap(). OnMap() takes in a point as a parameter, evaluates the X & Y values to see if the point is located on the map, and returns a Boolean(true or false).

 public bool OnMap(Point point)
    {
      return point.X >= 0 && point.X < Width && 
              point.Y >= 0 && point.Y < Height;

In a constructor of the MapLocation class, the OnMap(); method is called.

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

map.OnMap(this) is where the method is called, and it is passed this, which in this case represents a MapLocation object containing X & Y values. The purpose of MapLocation calling OnMap() is to determine if the current X & Y values are valid, meaning that they are located on the map. In order for the MapLocation constructor to call OnMap(), which is a public method, but located in a different class, MapLocation must take an instance of the Map class as a parameter.

Additionally, OnMap() must take in an instance of MapLocation inorder to access and evaluate the X & Y properties of the MapLocation object.

This might be a little confusing because OnMap() doesn't take MapLocation as a parameter, it takes Point as a parameter. OnMap() can also take MapLocation objects as a parameter because MapLocation is a child class that inherits from Point

class MapLocation : Point

Hope this helps.