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#

Alan Guilfoyle
Alan Guilfoyle
10,875 Points

Using The '(this)' Keyword

I aplogize for asking since it was explained in the video but I just can't wrap my brain around the keyword this usage. See comment: "THE USAGE OF 'this' HERE".

What is this represetning? I know it's an object...and I know it needs to be a Point object but I don't understand which Point object it's using or where it got it from. Also is there something else that could be passed in (more verbose option) that could take the place of this?

Here's the video pertaining to my question: https://teamtreehouse.com/library/c-objects/inheritance/throwing-exceptions

namespace TreehouseDefense {
    class MapLocation : Point {

        //base means parent/super class --- sub means child/derived class
        public MapLocation(int x, int y, Map map) : base(x, y) {

            if(!map.OnMap(this)) { //THE USAGE OF 'this' HERE
                throw new System.Exception();
            }
        }
    }
}

1 Answer

Brian DuPont
Brian DuPont
30,448 Points

Hi Alan,

I will walk you through a little bit of how my brain wraps itself around "this".

I will attempt to answer "I don't understand which Point object it's using or where it got it from".

First, a MapLocation is a Point, because MapLocation inherits from Point, so we can pass in a MapLocation as a parameter to the OnMap method.

Now, imagine we have a Map object called map created. We also have a MapLocation.cs class, that we can use to create instances of MapLocation. Let's create 3 instances of MapLocation ---> MapLocation L1 = new MapLocation(1, 2, map) , MapLocation L2 = new MapLocation(2, 2, map) , MapLocation L3 = new MapLocation(3, 2, map). Now when we run this code and it gets to the line where MapLocation L1 = new MapLocation(), the computer runs the objects class file (MapLocation.cs) to build the object. So in the class file the computer runs the constructor, which is were we need to check if the current MapLocation object (the object that is accessing the class file, or L1) is on the map or not. How do we pass in the current object that is accessing/using the class file to the OnMap method? We can't use our objects variable name ( map.OnMap(L1) ), even if that would work, it's not dynamic, what happens when our L2 and L3 object runs the class file? This is where "this" comes into play, we want the current object accessing/using the class file or "this" object. Now when the computer creates L1, "this" refers to the L1 object. When the computer creates L2, "this" refers to the L2 object, and so on.

So in short, the "this" keyword refers to the object accessing/using the class file. Like a placeholder/variable for the object itself.

I hope this helps a little. Maybe the brain of a struggling programmer can help out another.

Also is there a guide or some place where it explains how to use code blocks in and better format my answers for the future?

Alan Guilfoyle
Alan Guilfoyle
10,875 Points

Thanks for actually reading my question and not putting a blanket response. That does clarify it for me and I greatly appreciate it.

As for a guide to explain how to use code blocks and adding some formatting to your answers/responses. (I'm hoping you're referring to that) The community boards use the Markdown languge. Here's a handy cheatsheet that I refer too:

https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet

But in short to put a code block and have it stand out simply use the ` key three times. It's the button below the escape key, NOT the one to the left of the enter key.

Thanks again!