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
In this video, we'll review the solution to the third and final challenge.
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
How did it go?
0:00
Let's take a look at my solution.
0:01
In the Album.cs file,
I added a constructor
0:03
to the Album class, public Album.
0:08
Remember, a constructor
should have the same name
0:13
as the class that it's contained within.
0:16
Then you follow the name with a set of
parentheses and a set of curly braces.
0:19
Then I added a parameter for
each of the Album's required fields.
0:26
I only have two fields, Title and
Artist, and both of them are required.
0:32
So I added title and artist parameters,
both of type string.
0:37
Notice that I used a lowercase letter for
0:45
my parameter names,
just like I would for variable names.
0:47
Then I set the fields to
the parameter values.
0:52
So Title with a capital t =
title with a lowercase t.
0:55
Then Artist with a capital
A = Artist with lowercase a.
1:02
Because C# is case sensitive, the compiler
can understand that this Title
1:06
with a capital T refers
to the title field,
1:11
while this title with a lowercase
t refers to the title parameter.
1:15
Then I moved on to adding constructors
to the Book and Movie classes.
1:20
First, I'll save my file,
then open Book.cs.
1:25
Public, to make sure that
we can call the constructor
1:33
outside the class, Book,
parentheses, curly braces.
1:39
Then my parameters,
string title and string author.
1:44
Then initialize the field values
using the parameter values.
1:50
Title, title, then author,
1:55
oops, with a capital A, = author.
1:59
Cmd+S to save the file,
then open Movie.cs.
2:04
Public, again, Movie(), curly braces.
2:12
Add my parameters.
2:18
So string title and string director.
2:20
Then initialize the field values
with the parameter values.
2:26
Title with a capital T =
title with a lowercase t.
2:29
And Director, with a capital D,
= director with a lowercase d.
2:33
Command+S to save the file,
or Ctrl+S on Windows.
2:38
In the Program.cs file Main method,
2:42
I needed to pass the necessary arguments
to my immediate type class constructors
2:44
If I don't do this,
I'll get errors when I compile my program.
2:52
Since I already had the string literal
values here that I wanted to use,
2:56
I just copied and pasted each into
the appropriate constructor call.
3:00
Then I can remove these lines of code
that were setting the field values.
3:16
And then the movie.
3:31
First the title, Then the director.
3:35
Going to remove some white space just
to tighten this up a little bit.
3:43
There, that looks better.
3:48
Lastly, I compiled and ran my program.
3:50
And here's the expected output,
which is what we got last time.
4:00
But this time, our code is using
our media type class constructors
4:05
instead of setting
the field values directly.
4:10
Adding constructors to our media type
classes helps to make our code clearer and
4:12
more concise.
4:17
It's worth noting though that an object's
field value can still be changed after
4:18
its been instantiated.
4:23
Let's take a look at that.
4:24
After instantiating
this Album instance and
4:28
writing its field values to the console,
let's make a change.
4:32
Specifically, let's make
a change to the Title field.
4:38
I'll change the field value to
Let It Be instead of Yellow Submarine.
4:43
Then let's write the album field
values to the console again.
4:50
Save and compile, and run our application.
5:00
Here we can see the album
title Yellow Submarine,
5:07
which is what we passed
to the class constructor.
5:09
And here we can see the album title,
5:13
Let It Be, which is what we
changed the field value to.
5:15
As you saw in the C# objects course,
we can update our class field definitions
5:19
in order to prevent a field value from
being changed after it's been initialized.
5:24
To do that,
we just need to add the readonly keyword.
5:29
This may or may not be something
that makes sense to do.
5:37
Think about which object attributes
might change over time and
5:40
those that shouldn't change.
5:44
For my Album media type,
5:46
these field values don't have
a reason to change over time.
5:47
If they did change, it would more more
sense to create a new Album instance than
5:51
to update an existing Album.
5:55
The same logic applies to my
other media type classes,
5:57
so I'll make the same change
to those classes too.
6:01
And Movie.
6:09
Great job completing
this practice session.
6:15
Be sure to check out the next
session in this series
6:18
after you've completed the second
stage in the C# objects course.
6:20
Thanks for practicing with me,
and we'll see you next time.
6:25
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