1 00:00:00,380 --> 00:00:01,530 How did you do? 2 00:00:01,530 --> 00:00:03,630 Let's take a look at my solution. 3 00:00:03,630 --> 00:00:05,612 In each media type subclass, 4 00:00:05,612 --> 00:00:10,863 I replaced the GetDisplayText method with a DisplayText computed property. 5 00:00:10,863 --> 00:00:12,963 First, the Album class. 6 00:00:15,362 --> 00:00:19,389 Like I did in the walk through of my solution for the first challenge, 7 00:00:19,389 --> 00:00:24,920 I'll show an example of both the regular and expression body property syntax. 8 00:00:24,920 --> 00:00:29,420 I started with defining the computed property using regular property syntax. 9 00:00:33,090 --> 00:00:36,293 First, I stubbed out the property and its getter. 10 00:00:50,226 --> 00:00:54,918 Remember, we're creating a read-only property whose value is computed using 11 00:00:54,918 --> 00:00:56,095 other properties. 12 00:00:56,095 --> 00:00:59,055 So we only need to define a getter. 13 00:00:59,055 --> 00:01:02,271 For the properties getter implementation, I cut and 14 00:01:02,271 --> 00:01:06,357 pasted the GetDisplayText method body into the body of the getter. 15 00:01:15,668 --> 00:01:19,508 I'll select that code and press Tab to fix the indentation. 16 00:01:19,508 --> 00:01:23,352 And let's go ahead and remove what's left of the GetDisplayText method, 17 00:01:23,352 --> 00:01:24,764 as it's no longer needed. 18 00:01:30,564 --> 00:01:34,050 And that completed my DisplayText computed property. 19 00:01:35,230 --> 00:01:39,852 To convert this property to an expression body property, we need to rewrite 20 00:01:39,852 --> 00:01:44,132 the code and the getter as a single expression or a single line of code. 21 00:01:44,132 --> 00:01:48,584 While it's possible to do, the resulting code would not be very readable, so 22 00:01:48,584 --> 00:01:50,884 I'll hold off on showing that example. 23 00:01:50,884 --> 00:01:55,662 If you want to see what it'd look like, see the teacher's notes. 24 00:01:55,662 --> 00:01:59,869 But while I was working on converting the GetDisplayText method to a computed 25 00:01:59,869 --> 00:02:03,703 property, I noticed an opportunity to improve its implementation. 26 00:02:09,743 --> 00:02:14,717 This code here related to loading media library items is repeated in both 27 00:02:14,717 --> 00:02:17,300 the Book and Movie classes. 28 00:02:17,300 --> 00:02:20,705 So I move this code into the MediaType base class. 29 00:02:23,923 --> 00:02:27,898 To start, I added 30 00:02:27,898 --> 00:02:32,935 an OnLoanDisplayText 31 00:02:32,935 --> 00:02:37,711 computed property. 32 00:02:48,006 --> 00:02:51,922 Then I copied and pasted the repeated code into the getter. 33 00:03:01,479 --> 00:03:05,021 At this point, I had a little bit of clean up to do on this code. 34 00:03:10,810 --> 00:03:14,940 Instead of impending onto the text variable, which is no longer defined 35 00:03:14,940 --> 00:03:18,406 in this code block, I just returned these string literals. 36 00:03:24,381 --> 00:03:30,047 And I added an else statement and returned an empty string if the item isn't online. 37 00:03:41,816 --> 00:03:47,039 Then I switch back to the Album class and updated the DisplayText Computed property. 38 00:03:56,161 --> 00:03:58,945 We can now remove all of this code. 39 00:04:05,130 --> 00:04:08,908 And we can replace our text variable with the return keyword. 40 00:04:12,567 --> 00:04:17,583 And at the end of this line, we can append the OnLoanDisplayText property. 41 00:04:22,644 --> 00:04:26,245 Isn't that code easier to read and understand? 42 00:04:26,245 --> 00:04:27,741 After that change, 43 00:04:27,741 --> 00:04:32,670 it was easy to update the property to an expression body property. 44 00:04:32,670 --> 00:04:37,290 After the DisplayText property name I added =>, 45 00:04:37,290 --> 00:04:40,297 which is known as a fat arrow, and 46 00:04:40,297 --> 00:04:45,465 then removed everything up to the Album string literal. 47 00:04:52,438 --> 00:04:56,104 And after the OnLoanDisplayText property and the semicolon, 48 00:04:56,104 --> 00:04:58,748 I removed the last two closing curly braces. 49 00:04:58,748 --> 00:05:03,903 The line of code is a little bit long, so I'm gonna put my 50 00:05:03,903 --> 00:05:09,416 right before the album string literal and start a new line. 51 00:05:09,416 --> 00:05:13,190 And there, that's our completed expression body property. 52 00:05:14,390 --> 00:05:17,008 To update the other MediaType subclasses, 53 00:05:17,008 --> 00:05:21,760 I copied and pasted the expression body property to the Book and Movie classes. 54 00:05:29,057 --> 00:05:33,883 We need to change Album, here, to Book, and 55 00:05:33,883 --> 00:05:37,446 this Artist property to Author. 56 00:05:37,446 --> 00:05:39,983 Then we can remove the GetDisplayText method. 57 00:05:45,096 --> 00:05:46,595 Then the Movie class, 58 00:05:51,561 --> 00:05:57,175 Paste from the clipboard, Change this Album string to Movie. 59 00:05:59,135 --> 00:06:03,347 And replace the Artist property, here, with Director, and 60 00:06:03,347 --> 00:06:05,866 remove the GetDisplayText method. 61 00:06:12,666 --> 00:06:16,495 For the bonus challenge, I opened the MediaLibrary class file, 62 00:06:18,552 --> 00:06:23,359 And I stubbed out a number of items expression body computed property. 63 00:06:26,891 --> 00:06:30,630 Public, so it's accessible from outside of the class. 64 00:06:31,930 --> 00:06:33,663 Int for its data type. 65 00:06:33,663 --> 00:06:36,610 Then the name of the property, NumberOfItems. 66 00:06:39,480 --> 00:06:44,422 Then an = and a >, otherwise know as a fat error, 67 00:06:44,422 --> 00:06:48,799 then I return the length of the items array. 68 00:06:48,799 --> 00:06:52,846 So _items.link. 69 00:06:54,040 --> 00:06:56,958 Moving on to the Program.cs file. 70 00:06:59,253 --> 00:07:03,411 Scrolling down to the Display method, I replaced all the calls to 71 00:07:03,411 --> 00:07:07,276 the GetDisplayText method with the DisplayText property. 72 00:07:20,499 --> 00:07:26,814 And I added a call to the Console.WriteLine 73 00:07:26,814 --> 00:07:36,554 method in order to test the MediaLibrary NumberOfItems property. 74 00:07:49,444 --> 00:07:54,315 And lastly, I compiled and ran my program in order to test my changes 75 00:08:12,878 --> 00:08:16,618 Let's make one small change to make this output a little bit more readable. 76 00:08:19,732 --> 00:08:23,516 So Console.WriteLine, and then let's add a string literal here, 77 00:08:25,027 --> 00:08:29,002 # of items, followed by a colon and a space. 78 00:08:29,002 --> 00:08:30,385 I'll save the file, 79 00:08:37,268 --> 00:08:39,470 Compile and run my program again. 80 00:08:42,323 --> 00:08:44,795 There, that makes it a little bit more readable. 81 00:08:44,795 --> 00:08:48,043 So here's the number of items in our media library, and 82 00:08:48,043 --> 00:08:50,684 then here is the rest of the expected output. 83 00:08:52,387 --> 00:08:54,798 Congrats on completing this practice session. 84 00:08:54,798 --> 00:08:59,480 Be sure to check out the next session in the series after you've completed 85 00:08:59,480 --> 00:09:02,665 the six and final stage in the C# objects course. 86 00:09:02,665 --> 00:09:05,897 Thanks for practicing with me and we'll see you next time.