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 Methods

I feel like the public bool OnMap(Point point) part was not explained properly

The fact that whilst creating this bool method, it creates a new instance of the Point class was not really explained. I still cannot get my head around it. Also, the task after this is different as it takes an integer, where here it is of type Point.

Can someone please explain. Thanks

3 Answers

Steven Parker
Steven Parker
229,788 Points

The OnMap method described in the video does not create a new instance of the Point class. But it requires that a Point object be passed as a parameter.

The type and quantity of arguments passed to a method will vary depending on the purpose of the method. The techniques you are learning should apply to different situations, and you will be exposed to a variety in the course videos and challenges.

Yes I think I got confused because the word point is heavily used.

I just realised that that bool Method could even be written like:

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

because we created the Point class, and the Point constructor. Therefore it necessarily means when we create a new point object elsewhere, it has the properties of X and Y.

So even naming the argument for the bool method as 'whatever' still works

See if I've got this The upper case (Point...) is referencing the Point class and the lower case (point...) is telling the Point class what is wants it to do, which is "bool onBounds = point.X >= 0 && point.X < WIdth && point.Y >= point.Y < Height; return inBounds;"

If my explanation make sense.

Daniel W
Daniel W
1,341 Points

The easiest way to not get confused is to think of Point as being there instead of any other type declaration like "string" or "int". The "whatever" is of the type Point, but it can also be called point. By convention as explained in the videos, you use all lower case letters for method parameters. This "whatever" of yours is a reference to whatever was passed into the function from anywhere else in the code, and lives within the function. Where as if it has a capital letter, you know it's either a class or that an instance variable.

Md shujaul Haque
Md shujaul Haque
839 Points

why we use x,y variable .... like point.x and pint.y ........ x and y are not initialize in the field . ??