Wrapping Up9:05 with James Churchill
In this video, we'll review the solution to the second and final challenge.
DisplayText Computed Property
Here's what the Album
DisplayText property looks like rewritten as an expression-bodied property before adding the
OnLoanDisplayText computed property to the MediaType base class.
public string DisplayText => "Album: " + Title + " by " + Artist + (OnLoan ? !string.IsNullOrEmpty(Loanee) ? " (Currently on loan to " + Loanee + ")" : " (Currently on loan)" : "");
How did you do? 0:00 Let's take a look at my solution. 0:01 In each media type subclass, 0:03 I replaced the GetDisplayText method with a DisplayText computed property. 0:05 First, the Album class. 0:10 Like I did in the walk through of my solution for the first challenge, 0:15 I'll show an example of both the regular and expression body property syntax. 0:19 I started with defining the computed property using regular property syntax. 0:24 First, I stubbed out the property and its getter. 0:33 Remember, we're creating a read-only property whose value is computed using 0:50 other properties. 0:54 So we only need to define a getter. 0:56 For the properties getter implementation, I cut and 0:59 pasted the GetDisplayText method body into the body of the getter. 1:02 I'll select that code and press Tab to fix the indentation. 1:15 And let's go ahead and remove what's left of the GetDisplayText method, 1:19 as it's no longer needed. 1:23 And that completed my DisplayText computed property. 1:30 To convert this property to an expression body property, we need to rewrite 1:35 the code and the getter as a single expression or a single line of code. 1:39 While it's possible to do, the resulting code would not be very readable, so 1:44 I'll hold off on showing that example. 1:48 If you want to see what it'd look like, see the teacher's notes. 1:50 But while I was working on converting the GetDisplayText method to a computed 1:55 property, I noticed an opportunity to improve its implementation. 1:59 This code here related to loading media library items is repeated in both 2:09 the Book and Movie classes. 2:14 So I move this code into the MediaType base class. 2:17 To start, I added 2:23 an OnLoanDisplayText 2:27 computed property. 2:32 Then I copied and pasted the repeated code into the getter. 2:48 At this point, I had a little bit of clean up to do on this code. 3:01 Instead of impending onto the text variable, which is no longer defined 3:10 in this code block, I just returned these string literals. 3:14 And I added an else statement and returned an empty string if the item isn't online. 3:24 Then I switch back to the Album class and updated the DisplayText Computed property. 3:41 We can now remove all of this code. 3:56 And we can replace our text variable with the return keyword. 4:05 And at the end of this line, we can append the OnLoanDisplayText property. 4:12 Isn't that code easier to read and understand? 4:22 After that change, 4:26 it was easy to update the property to an expression body property. 4:27 After the DisplayText property name I added =>, 4:32 which is known as a fat arrow, and 4:37 then removed everything up to the Album string literal. 4:40 And after the OnLoanDisplayText property and the semicolon, 4:52 I removed the last two closing curly braces. 4:56 The line of code is a little bit long, so I'm gonna put my 4:58 right before the album string literal and start a new line. 5:03 And there, that's our completed expression body property. 5:09 To update the other MediaType subclasses, 5:14 I copied and pasted the expression body property to the Book and Movie classes. 5:17 We need to change Album, here, to Book, and 5:29 this Artist property to Author. 5:33 Then we can remove the GetDisplayText method. 5:37 Then the Movie class, 5:45 Paste from the clipboard, Change this Album string to Movie. 5:51 And replace the Artist property, here, with Director, and 5:59 remove the GetDisplayText method. 6:03 For the bonus challenge, I opened the MediaLibrary class file, 6:12 And I stubbed out a number of items expression body computed property. 6:18 Public, so it's accessible from outside of the class. 6:26 Int for its data type. 6:31 Then the name of the property, NumberOfItems. 6:33 Then an = and a >, otherwise know as a fat error, 6:39 then I return the length of the items array. 6:44 So _items.link. 6:48 Moving on to the Program.cs file. 6:54 Scrolling down to the Display method, I replaced all the calls to 6:59 the GetDisplayText method with the DisplayText property. 7:03 And I added a call to the Console.WriteLine 7:20 method in order to test the MediaLibrary NumberOfItems property. 7:26 And lastly, I compiled and ran my program in order to test my changes 7:49 Let's make one small change to make this output a little bit more readable. 8:12 So Console.WriteLine, and then let's add a string literal here, 8:19 # of items, followed by a colon and a space. 8:25 I'll save the file, 8:29 Compile and run my program again. 8:37 There, that makes it a little bit more readable. 8:42 So here's the number of items in our media library, and 8:44 then here is the rest of the expected output. 8:48 Congrats on completing this practice session. 8:52 Be sure to check out the next session in the series after you've completed 8:54 the six and final stage in the C# objects course. 8:59 Thanks for practicing with me and we'll see you next time. 9:02
You need to sign up for Treehouse in order to download course files.Sign up