Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
What does it mean for two objects to be equal?
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
According to the rules of the game, towers
can't be placed directly on the path.
0:00
We need a way to know if a map
location is on the path.
0:04
Let's add another method to the Path
class that will return true,
0:08
if the map location passed in,
is on the path.
0:12
So we'll say public bool IsOnPath and
0:15
it will take a MapLocation as a parameter.
0:19
And this method, will loop through all
of the locations in the path array.
0:26
So we'll say,
foreach(var pathLocation in _path,
0:31
And if the location passed in
equals one of the path locations,
0:40
We'll return true immediately.
0:48
Otherwise, if we get through all
of the locations on the path and
0:52
we get down here to the end of
the four H loop we'll return false.
0:56
Because we didn't find a matching
location in the path array.
1:01
To test this, let's create a map
location that is on the path.
1:05
For now,
we'll just do this in the Game.cs file.
1:09
So here we have our path.
1:13
Under here,
let's create a new MapLocation.
1:15
And we'll have it be right at
the beginning of the path, so
1:22
right here at 0, 2.
1:26
Just copy that down there.
1:31
Now we can say if(path.IsOnPath and
1:33
pass in that location we just created.
1:37
Then on the console will print,
1:46
location + " is on the path".
1:53
And just so we don't continue down into
our program we'll return right here.
2:00
Let's go ahead and compile and run this.
2:06
Hm.
2:18
We didn't see our message.
2:19
We should have seen this
message printed to the console.
2:21
And we shouldn't have seen any of this
stuff because we would have returned.
2:29
We must have a bug in the IsOnPath method.
2:33
Let's take a second look.
2:36
On the surface it looks correct,
doesn't it?
2:37
There's something very
subtle happening here.
2:41
This double equals sign checks to see if
two variables refer to the same object.
2:43
This is different than two locations
on the map being the same.
2:50
Let's go back to Game.cs to see
the MapLocation objects that should have
2:54
caused the IsOnPath method to return true.
2:58
Here's the MapLocation that's
in the path array, and
3:06
here's the MapLocation that
we're checking against.
3:09
They may look the same, but these
are actually two very different objects.
3:13
See, here we created a new MapLocation and
put it in this array, and
3:18
down here we created a completely
different MapLocation.
3:22
They just so happen to refer to
the same location on the map.
3:26
It's like they're identical twins.
3:30
They aren't the same person,
but they look identical.
3:32
The equality operator by default,
3:36
only checks to see if they
are the exact same object.
3:38
Or in the case of twins,
the exact same person.
3:42
On the computer, these two objects
sit in different locations in memory.
3:45
The equality operator actually just calls
the static, system.object.ReferenceEqual
3:50
method to determine if two variables
refer to the same location in memory.
3:55
In which case, they are the same object.
4:00
If two variables refer to
the same object in memory,
4:03
we can say that they
have reference equality.
4:06
That's all that double equals
equality operator checks for.
4:09
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up