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 Inheritance Throwing Exceptions

jas cheema
jas cheema
823 Points

third parameter in the constructor

I can not understand why Map map is passed to MapLocation constructor

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

2 Answers

Allan Clark
Allan Clark
10,810 Points

Map is needed in the constructor to verify that the x and y parameters are valid. The constructor tests that (x,y) coordinate is on the map passed in. Don't worry about the error he was talking about, he was referring to using the keyword 'this'. The OnMap method needs an x and y on the Point passed to it, the video means you will not get an error by passing 'this' to OnMap because the base constructor will have already run.

jas cheema
jas cheema
823 Points

Thanks a lot Allan. But sorry I am still confused. Can you please explain with an example?

Allan Clark
Allan Clark
10,810 Points

For instance you can't make a MapLocation at (12, 14) if the map is only a 10x10 map. You would need have the map before you created the location, so the easiest way to check that is in the constructor.

There is a couple ways to look at the use of Map.map. First, you are looking for a map location, so you need a map to verify against. Second, OnMap is a method of the Map class, so you need an instance of Map to call OnMap.

jas cheema
jas cheema
823 Points

I don't understand at all. how can we pass an object of class to constructor? according to video it was to violate something to invoke default constructor.Can you please explain in details ?