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 Methods Return Values

Ira Buckman
Ira Buckman
1,447 Points

Question about the syntax of the call to the OnMap method

Greetings,

At 2:38 of this video Jeremy calls the OnMap method from Game.cs.

Line 11 reads: bool isOnMap = map.OnMap(point)

I don't understand the syntax in which map. preceded OnMap. map passes the Width & Height previously created by the call to the Map method which created the map. So shouldn't it be included as an argument inside the parens as follows?:

bool isOnMap = OnMap(map, point)

Please enlighten me!

Thanks so much.

Ira Buckman

2 Answers

Zhaopeng Wang
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Zhaopeng Wang
Full Stack JavaScript Techdegree Graduate 32,210 Points
  • You have to do
 bool isOnMap = map.OnMap(point)
  • Because you are calling method you write in Map.cs file from Game.cs file. There is no OnMap method defined in Game.cs.
  • Notice, in Game.cs file, you are initialize a new Map by calling Map map = new Map();
  • the map here is just the name of the new Map object, you can give the map another name, such as
Map someGameMap = new Map(8, 5);

And, then you need to use

someGameMap.OnMap(map, point);
Ira Buckman
Ira Buckman
1,447 Points

Hi Zhaopeng,

Thank you very much for your reply. Are you saying that whenever a class calls a method that has been defined in a different class the call must indicate the class where the method exists?

Is that what "map." in map.OnMap(point) does? Is "map." indicating that the OnMap method exists in class Map?

Thank you very much.

Ira Buckman

Zhaopeng Wang
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Zhaopeng Wang
Full Stack JavaScript Techdegree Graduate 32,210 Points
  • The short answer is: you have to initialize the class first to be able to use the method defined in that class.
  • It is a typically Object Oriented Programming concept, Encapsulation.
Ira Buckman
Ira Buckman
1,447 Points

Thanks, Zhaopeng - that is very helpful.