Adding a Method Overload5:48 with James Churchill
In this video, we'll review the solution to the second challenge and introduce our third and final challenge—adding a method overload.
- Add a method overload for the
Loan()method that doesn't accept any parameters.
- Update the
GetDisplayText()method to account for when the
Loaneefield doesn't have a value.
Addition Assignment Operator (+=)
For more information about the addition assignment operator (
+=) see https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/addition-assignment-operator.
If you get stuck on any of the following topics or simply need a refresher, click on a topic in list below to view the associated video in the C# Objects course.
How did it go with the second challenge? 0:00 Let's walk through my solution. 0:02 In the album class, I stubbed out the loan and return methods. 0:04 Public void Loan() And public void Return() 0:08 Since the methods won't be returning a value to the caller, 0:23 I use the special keyword void for the method return type. 0:26 To complete the loan method, I added a string parameter named loanee. 0:32 Defining this parameter will allow the caller 0:39 to provide the name of the person who's borrowing the item. 0:42 At this point, I remembered that I needed to add two new fields to the album class. 0:45 A string field named Loanee And a bool field namedOnLoan. 0:52 And then, back in the Loan method, 1:05 I set the loanee field to the loanee parameter value. 1:07 And the OnLoan field to true to indicate that the item is currently on loan. 1:15 To complete the return method, 1:24 I reset the loan-related fields to their default values. 1:26 Null for the Loanee field And false for the OnLoan field. 1:29 To indicate that the item was returned to the media library. 1:37 A quick note about the default values for the Loanee, and OnLoan fields. 1:45 The default values for these fields work for 1:50 our needs when an item is initially created and added to the media library. 1:52 It shouldn't have a Loanee field value. 1:57 And the OnLoan field should be set to false, given that we 2:00 don't need to initialize these fields to something other than their default values. 2:04 Stylistically, you could explicitly set them to their default values. 2:09 Doing that might make your intentions clearer 2:14 to anyone else who's reading your code. 2:17 If you like, you can set explicit default values here where the fields are defined. 2:24 Or you can do that in the constructor. 2:29 But for now, I'm just going to let the default values to be set implicitly. 2:32 Continuing on. 2:40 In the GetDisplayText method, 2:41 I updated it to include the loan related fields if the item is out on loan. 2:43 I started with declaring a string variable named text. 2:53 For the text to return instead of immediately returning it from the method. 3:00 Then, I added an if statement in order to check if the OnLoan field 3:05 is set to a value of true. 3:09 If OnLoan, then a set of curly braces. 3:11 And if the OnLoan field value evaluates to true, 3:17 I appended the following text onto the text variable, 3:23 text = text + " (Currently on loan to 3:29 Loanee, And then a close parenthesis. 3:36 You could also use the addition assignment operator here. 3:42 This operator says add the text variable to this string value, and 3:49 assign the result back to the text variable. 3:54 You'll see this approach used frequently when a string needs to be appended 3:58 to another string. 4:01 And don't forget to return the text variable. 4:04 To test my changes, I updated the Program.cs file to call the loan and 4:09 return methods. 4:14 Let's first write the media type information to the console, 4:17 then loan out the first album. 4:21 Remember, we need to pass the name of the loanee to the method call. 4:26 How about Joe Smith? 4:30 Then, let's write the media type information for 4:34 album1 to the console again. 4:36 And then, let's return the album. 4:39 Album1 Return. 4:42 And one more time. 4:46 We'll write the media type information for the album to the console. 4:48 And lastly, I compiled and ran the program. 4:51 Here, we can see the display text before the item was loaned out. 4:59 And then, when the item was loaned to Joe Smith. 5:05 And here's the display text after the item was returned. 5:09 Looks like everything is working as expected. 5:14 For the third and 5:17 last challenge in this practice session, let's make it possible to indicate that 5:18 something is on loan without having to supply the loanee's name. 5:22 To do this, you'll add a method overload for the loan method. 5:26 The current method accepts a Loanee parameter, so 5:30 you'll add a method overload with a loan method that doesn't accept any parameters. 5:33 Also, update the GetDisplayText() method to account for 5:39 when the Loanee field doesn't have a value. 5:42 And that's it for the third challenge. 5:45 We'll see you in a bit. 5:47
You need to sign up for Treehouse in order to download course files.Sign up