Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
      You have completed C# Objects!
      
    
You have completed C# Objects!
Preview
    
      
  Properties can be used the same way fields are, but they're really more like methods.
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
                      With the setter of the location
property being private,
                      0:00
                    
                    
                      the only way it can be set is
from within the invader class.
                      0:03
                    
                    
                      Right now, we don't have anything
that sets the location property.
                      0:07
                    
                    
                      The default value of an uninitialized
map location is null.
                      0:11
                    
                    
                      We don't want users of the invader
class getting null when they ask for
                      0:15
                    
                    
                      the invader's location.
                      0:18
                    
                    
                      We need to initialize it to some starting
value when the object's created.
                      0:19
                    
                    
                      That's the purpose of the constructor.
                      0:23
                    
                    
                      So let's add a constructor.
                      0:25
                    
                    
                      So we'll type public Invader.
                      0:28
                    
                    
                      The constructor will set
the location property to the first
                      0:31
                    
                    
                      location on the path.
                      0:33
                    
                    
                      So the constructor needs
to have the path passed in.
                      0:35
                    
                    
                      We'll get the GetLocation method of the
path object to get the first location on
                      0:38
                    
                    
                      the path and assign it to location.
                      0:42
                    
                    
                      So now, all invaders will start
on the first step of the path.
                      0:49
                    
                    
                      [SOUND] In the tree house defense game,
                      0:53
                    
                    
                      invaders move down the path while
towers attempt to neutralize them.
                      0:55
                    
                    
                      If the invader successfully reaches the
end of the path, then the game is over and
                      1:00
                    
                    
                      the player will have lost.
                      1:04
                    
                    
                      It would be the responsibility of each
invader object to keep track of how far
                      1:06
                    
                    
                      down the path they are.
                      1:10
                    
                    
                      So, we'll need to add another field
called _pathStep to keep track of this.
                      1:11
                    
                    
                      No other classes will need
to know this information.
                      1:16
                    
                    
                      So we'll make it private.
                      1:18
                    
                    
                      This field will change over time.
                      1:22
                    
                    
                      So we don't want to make it read-only.
                      1:24
                    
                    
                      We'll also give this field
an initial value of 0.
                      1:27
                    
                    
                      Because all invaders will
start on step 0 of the path.
                      1:30
                    
                    
                      In fact we can use this _pathStep
field in our call to GetLocationAt,
                      1:34
                    
                    
                      instead of using 0 twice.
                      1:38
                    
                    
                      The reason we can do this is
because the _pathStep field is
                      1:40
                    
                    
                      set to 0 before the Invader
constructor is called.
                      1:43
                    
                    
                      The fields of a class are always
                      1:46
                    
                    
                      initialized before
the constructor is called.
                      1:48
                    
                    
                      Of course, the default value
of an integer is always 0,
                      1:50
                    
                    
                      so initializing it to 0 here is redundant.
                      1:54
                    
                    
                      But it's a good practice
to initialize it to 0.
                      1:58
                    
                    
                      By doing this we're saying that we've
thought about what we want this initial
                      2:01
                    
                    
                      value to be, and we want it to be 0.
                      2:04
                    
                    
                      Now we'll create a method called move
that'll advance the invader down the path.
                      2:08
                    
                    
                      This will be called by code outside the
invader class, so it needs to be public.
                      2:13
                    
                    
                      We'll strictly use this method
to tell the invader to move, so
                      2:18
                    
                    
                      we don't need anything back from it.
                      2:21
                    
                    
                      Let's give it a void return type.
                      2:23
                    
                    
                      When the move method is called,
                      2:27
                    
                    
                      it will advance the invader
one step down the path.
                      2:29
                    
                    
                      To do that, we just need to
increase the _pathStep by 1.
                      2:33
                    
                    
                      Now that we've changed what
step the invader is on,
                      2:37
                    
                    
                      we need to update its location.
                      2:40
                    
                    
                      We can do this using
path.GetLocationAt method.
                      2:42
                    
                    
                      The only problem is, we don't have
a path object here to refer to.
                      2:46
                    
                    
                      It looks like we need to store an instance
of the path object in the invader object
                      2:50
                    
                    
                      so that it can be accessed later.
                      2:54
                    
                    
                      To do this, we just need to create
another field in the Invader class and
                      2:57
                    
                    
                      assign it the path that was
passed into the constructor.
                      3:00
                    
                    
                      The path object is just for
the Invader class to use.
                      3:03
                    
                    
                      So we'll make it private readonly.
                      3:06
                    
                    
                      Then in the constructor we'll set
it to the path that was passed in,
                      3:12
                    
                    
                      and I'll change this to
use the field instead.
                      3:17
                    
                    
                      Now we can go back to the move method and
use the object's instance of the path
                      3:23
                    
                    
                      to update the location property
to the invader's new location.
                      3:27
                    
                    
                      So we'll say location =
_path.GetLocationAt(_pathStep).
                      3:31
                    
                    
                      There we go.
                      3:43
                    
                    
                      Finally, a note about terminology.
                      3:45
                    
                    
                      We've now created classes that have
fields, properties, methods, and
                      3:47
                    
                    
                      constructors.
                      3:52
                    
                    
                      Each of these things are called
members of the class.
                      3:53
                    
                    
                      We can refer to the path and
pathStep fields, the location property and
                      3:57
                    
                    
                      the Move method collectively as
members of the Invader class.
                      4:02
                    
                    
                      Members that are treated like variables
in the class, such as fields and
                      4:07
                    
                    
                      properties, are often referred
to as member variables.
                      4:11
                    
                    
                      In other courses we'll learn about other
types of members that a class can have.
                      4:15
                    
              
        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