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 Practice Methods in C#!
You have completed Practice Methods in C#!
Preview
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 you do?
0:00
Let's take look at my solution.
0:01
In album class,
I added second method named loan.
0:03
Public void Loan And just
0:10
like before since this method won't return
a value, I used void for the return type.
0:16
Actually, when overloading a method,
the return types have to match.
0:22
If they don't match,
the C# compiler will generate an error.
0:27
Since this version of the method
will be used to loan out an item
0:31
without supplying the loanee's name,
we don't need to define any parameters.
0:34
For the method implementation
we could just repeat the code
0:40
to set the online field to true.
0:43
Or we can call the new loan
method from the old load method.
0:48
Notice though that now we're setting
the on loan field to true in two
0:54
different places here and here.
0:58
Inside of this method overload,
instead of setting on loan field to true,
1:01
let's just call the other method.
1:06
Doing it this way helps to keep
our code from containing any
1:11
unnecessary duplication.
1:15
The approach of calling a method
overload from within a method overload
1:17
is something that's commonly done.
1:21
Next, I updated the GetDisplayText
method to account for
1:23
with the Loanee field
doesn't have a value.
1:27
So, if Loanee does not equal null and
1:34
Loanee does not equal empty string.
1:40
Adding the second check to make sure that
the Loanee field isnβt set to an empty
1:50
string makes our code more resilient.
1:54
This is a very common thing to do to
check to see if a string is not null and
1:57
not empty.
2:02
This is so common,
that the .net framework string data type
2:03
includes a special method that
we can use for this purpose.
2:07
String is null or empty.
2:14
Then you pass in the string
that you want to check.
2:19
In this case, the Loanee field.
2:22
Since this method checks
that the string is null or
2:24
empty, we need to add the not or
the logical negation operator
2:26
before the method call in order
to negate the return value.
2:31
So if the method returns false, meaning
that the string isn't null or empty,
2:36
the not operator will flip that to true
and satisfy our conditional statement.
2:42
So if we have a loanee field value,
2:48
then I append the text like
we previously were doing.
2:50
And if we don't have a Loanee field value,
2:54
we can use this else block to
append the following text.
3:00
Currently on loan.
3:12
And that's the best we can do.
3:17
We simply don't know the Loanee's name.
3:18
In the program.csfile main method,
3:20
I added a call to the loan method overload
and another call to the return method.
3:23
Album2.Loan, this time,
3:31
without passing any argument,
and album2 Return.
3:34
I also added two additional
console writeline method calls.
3:41
Here, let's write album2's information
to the console, and down here.
3:46
And lastly, I compiled and ran my program.
3:54
And here's the expected output.
4:04
Here's when an item is out on loan and
we know the loanee name.
4:07
And here's when an item is out on loan and
we don't know the loanee's name.
4:11
Excellent job completing
this practice session.
4:15
Be sure to check out the next
session in the series
4:18
after you've completed the third
stage in the C# objects course.
4:20
Thanks for practicing with me and
we'll see you next time.
4: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