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 trialAbderRahman Ezzeldin
1,066 Pointswhat does the "this" do ?
what does the "this" do exactly ? and why do we have to use it in this case ?
3 Answers
Steven Parker
231,275 PointsInside a constructor, "this" refers to the object that is being created.
Since the "base" has already loaded in the values for "X" and "Y", the object can be passed to the "Map" function "onMap" to determine if the coordinates are valid.
Ricardo Ponce
1,705 Pointsok As You said: Inside a constructor, "this" refers to the object that is being created, so in that case what is the object that is created? ...MapLocation?
Steven Parker
231,275 PointsBased on the code shown about 2 minutes into this video, you're right, the object that would be created would be an instance of MapLocation.
Ricardo Ponce
1,705 Pointsforget Python for that and answer the question because I have the same question, if I don't want to write the keyword "this", what object I would write
Steven Parker
231,275 PointsAs I said, "this" has special meaning in C#, so you can't substitute anything else.
Marc Martinez
12,909 PointsMarc Martinez
12,909 PointsWould that be the same as "Self" in Python?
Steven Parker
231,275 PointsSteven Parker
231,275 PointsNot quite. In Python, using the name "self" is just a convention but in C# "this" has special meaning to the compiler.
But how they are used is similar. Assuming the Python code is conventional, they both refer to the current object instance.