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
Try your hand at writing properties on your own. Then check your work as we do it together.
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
Let's finish up the invader class by
adding a couple more properties that will
0:00
come in handy.
0:03
This will give us more
practice writing properties.
0:05
We need to think about what else
other classes will need to know about
0:08
the invader.
0:11
For one, we'll want to know if it's scored
by making it to the end of the path.
0:12
We can figure out if the invader
has scored by using the path and
0:17
path step fields.
0:20
This is a good case for
a computer property.
0:22
Let's call it, HasScored.
0:25
Actually you know what,
I think with what you've learned so
0:27
far about properties,
you can write the rest of this yourself.
0:31
Before you do, I'll give you a hint,
in order to write this property,
0:35
you'll need to actually add another
property to the path class.
0:39
Let me write a comment here just to
remind you what this property should do.
0:43
Now pause the video,
code up this property and
0:51
when you come back we'll walk
through an implementation together.
0:54
All right, we're back.
1:00
How do you think you did?
1:02
Does your HasScored property return true
1:04
if the invader has reached
the end of the path?
1:06
This one was a little tricky.
1:09
Let's work through it together.
1:11
We need to check if pathStep is greater
than or equal to the length of the path.
1:13
This is how we'd write that.
1:25
Because we're learning
how to write properties,
1:28
I've written this one with the more
verbose property syntax with curly braces.
1:31
Notice that I can write
it all on a single line.
1:35
For simple properties like this,
this is a common practice.
1:38
The property reads invader has scored
if the path step is greater than or
1:42
equal to the path's length.
1:47
Notice that this property is calling
the length property of the path.
1:50
The problem is the path class
doesn't have a length property, so
1:53
we'll need to add it.
1:58
Let's go over to the path class and
add another computed property.
2:00
This time, let's use the even simpler
syntactic sugar we learned for
2:05
writing a property on a single line.
2:09
All this property does is return
the length of the path array
2:12
that's encapsulated in the path class.
2:16
Now let's compile to make sure
we typed everything right.
2:20
There you go,
that's the HasScored property.
2:27
If you're just learning about properties,
then this might have been a bit tricky.
2:30
That's all right, we're here to learn.
2:35
This is how I learn to code.
2:37
I often know what I want to do but
I don't always know how to do it.
2:39
That gives me an opportunity
to figure it out.
2:44
This almost always requires
learning something new.
2:47
Learning how to do stuff is both
challenging and fun and I feel great
2:51
after I've solved the problem,
no matter how trivial it might be.
2:55
Let's do one more.
2:59
Once an invaders health has decreased
to zero, it's no longer a threat.
3:01
The rest of the game needs to determine
if an invader has been neutralized.
3:05
Other parts of the game could check
if the health is zero or less.
3:10
There's a good chance that this check
will need to be done in many places
3:14
throughout the code.
3:17
This sounds like another great case for
a computed property.
3:19
Let's add another property named
IsNeutralized that returns
3:22
true if the invaders health is zero or
less, and false otherwise.
3:27
Pause the video here, and
fill out the rest of this property.
3:31
When you come back I'll show
you the finished code for
3:35
the property in case you
want to check your work.
3:38
All right, here's what I wrote, since
this property is a single line of code I
3:44
decided to write it using the syntactic
sugar we learned about earlier.
3:49
Also notice that to be safe, I'm not
just checking if health equals zero.
3:54
It's always best to make
comparisons as general as possible.
4:00
So I've decided to use less than or
equal to.
4:04
That was easy right?
4:07
You've probably started to notice
a pattern in the way I've named these
4:22
properties.
4:26
They either start with is or has.
4:27
This is a common naming in practice
when writing Boolean properties.
4:30
These properties are answering yes and
no questions about the invader object.
4:34
Naming them this way makes the code that
uses these properties read a little more
4:39
like English cuz you can write code
that reads if invader is neutralized or
4:44
if invader has scored.
4:49
Before we write any more code,
be sure to compile and fix any errors.
4:52
In this section of the course,
we've learned a lot about properties.
4:57
We've seen how methods and properties
are used to do good encapsulation.
5:01
We're almost done with
the tree house defense game.
5:05
Let's put what we've learned about
object oriented programming in C# to
5:08
use while we finish it up.
5:11
We'll learn a bunch more in the process.
5:13
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