Adding Computed Properties11:25 with James Churchill
In this video, we'll review the solution to the first challenge and introduce our second challenge—adding computed properties to our media type classes.
In each media type subclass, replace the
GetDisplayText()method with a
- Use either the regular C# property syntax or the more concise expression-bodied property syntax.
In the MediaLibrary class, add a
NumberOfItemscomputed property that returns an integer representing the number of items in the media library.
Update the Program.cs file as needed.
- Also add code to the
Main()method in order to test the new MediaLibrary
- Also add code to the
If you need a refresher on expression-bodied members in C#, checkout the following documentation.
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.
Welcome back, how did you do? 0:00 Let's walk through my solution, I started with the media type base class. 0:02 There were three public fields to replace with properties, 0:10 Title, Loanee, and Onloan. 0:15 For the sake of this walkthrough, I'll show an example of both the regular and 0:18 auto implemented property syntax. 0:22 I started with converting the title field to a property using regular property 0:25 syntax, I changed the access modifier to private. 0:29 We can do this because the field will now be the backing store for the property. 0:35 All access to the field will be done through the associative 0:40 property skidder and setter and I renamed the field to _Title. 0:45 Remember that following the naming convention of prefixing private 0:51 field names with an underscore makes it easier to 0:55 identify private fields throughout your code. 0:59 Then, I added the associated property right below the back infield. 1:04 And then I added the getter. 1:14 And, the setter. 1:20 Before going any further, I saved the file and attempted to compile the program. 1:28 And the program successfully compiled. 1:48 Notice though that the title back in field isn't marked as being read only, 1:57 that feels like an oversight to me. 2:01 The title field probably shouldn't change it's value once it's been set. 2:03 Let's add the read only keyword in order to make the field read only, 2:08 so, private readonly string_title, and let's try compiling our program again. 2:14 And this time, I've received a compilation error. 2:29 A read-only field, Treehouse.MediaLibrary.MediaType._title 2:34 cannot be assigned to (except in a constructor or a variable initializer). 2:40 This compilation error is the result of having a read-only back in field while 2:46 trying to assign a value in the associated property setter. 2:50 To resolve this issue, I needed to remove the property setter, 2:59 and set the backing field in the constructor. 3:04 After making those changes, I attempted to compile the program again. 3:15 And this time it succesfully compiled. 3:26 Initially this might seem strange, but it does make sense. 3:33 Since the backing field is marked as readonly the associated property should 3:41 also be read only by only defining a getter. 3:45 I then switched to using the more concise auto implemented property syntax. 3:48 First, I selected and commented out the backing field and get property. 3:55 And added the auto implemented property. 4:04 I also updated the initialization code in the constructor. 4:15 When using an auto-implemented property, 4:22 you can't directly reference the backing field. 4:25 So we need to just reference the property setter here. 4:27 At this point, I defined the auto-implemented property, but 4:37 still needed to make it read-only. 4:39 I no longer have the explicit back and fill to mark this read only and 4:42 it's not possible to add the read only key word to a property. 4:46 The best that I could do is add a private access modifier to the setter making it 4:49 only accessible to the code within this class. 4:54 This approach didn't give me exactly what I had with the regular property, 5:03 it's still possible for code within the class to change the value of the property 5:06 after the class has been initialized by the instructor. 5:11 But from outside of the class, the behavior is the same as 5:14 the read only public field or the getter only regular property. 5:18 Once the property value has been set via the call to the constructor. 5:23 It can't be change because the property setter is private. 5:29 For most use cases, this is close enough to the original behavior, but 5:35 this limitation of using the auto-implemented property syntax 5:38 is something to be aware of. 5:42 Next, I replace the Loanee and OnLoan fields with auto-implemented properties. 5:47 Both of these properties only need to be set from within this class, so 6:04 I went ahead and made their setters private. 6:08 Then I moved on to the media type subclasses, 6:13 first, the album class. 6:20 I converted the artist field to a property. 6:27 Remove the read only keyword and add get set. 6:37 Since this field is read only I made the setter private. 6:49 Then in the book class I converted the author field to a property. 6:55 Remove the read only keyword and add get set. 7:04 And again we can make the set a private. 7:12 Then, in the movie class, 7:14 I converted the directive field to another property, 7:17 remove the read only keyword and add a get and a set. 7:23 And one more time, we can make the set a private. 7:32 To finish the challenge, I just needed, 7:36 at this point, to add the code in the immediate type base class, 7:39 to initialize the Loanee, and OnLoan properties. 7:44 To do that in line, I just added the initial values, 7:51 right after the property definitions. 7:54 We'll initialize the Loanee property to an empty 7:58 string and the OnLoan property to false. 8:04 After making that last change, I made sure all my 8:14 files were saved and I compiled and ran my program. 8:19 And here's the expected output. 8:33 Nice job completing the first challenge in this practice session. 8:40 Now that we've seen how to replace class fields with properties, 8:44 let's explore using computed properties to add attributes whose values 8:47 are based upon other attributes. 8:52 Before we introduce the next challenge, let's take a closer look at the process of 8:55 determining if a method can be converted to a computer property. 8:59 Ask yourself the following questions:, does the method return a value? 9:05 A computer property by its very definition has to return a value. 9:10 So a method that has void for its return type isn't a good candidate for 9:14 converting to a computer property. 9:19 Does the method accept arguments? 9:21 It's not possible to pass arguments to a computer property. 9:24 So, if a method accepts arguments, it's not a candidate for 9:28 converting to a computer property. 9:31 Is calling the method a relatively inexpensive operation 9:35 that returns quickly. 9:39 Consumers of classes will, in general, expect that getting a properties value 9:41 will be an inexpensive and quick operation. 9:45 If a method has to perform expensive operations or calculations, 9:48 in order to determine its return value It's probably not a good candidate for 9:53 conversion to a computer property. 9:59 Looking at the GetDisplayText method, 10:03 in the album class, we can see that it returns a string guy. 10:07 And doesn't accept any arguments, and 10:15 determining its return value doesn't seem to require any expensive operations. 10:18 For the second challenge let's convert this method to a computed property. 10:25 And each media type sub class replaced the GetDisplayText method with the display 10:35 text computer property. 10:40 Feel free to use either the regular C# property syntax or 10:41 the more concise expression-bodied properties syntax. 10:45 And as a bonus challenge let's add a new computer property in the media 10:51 library class, add a number of items computer property that returns 10:56 an integer representing the number of items in the media library. 11:01 And finally, update the Program.cs file as needed, in order for 11:06 your program to successfully compile also add code to the main method in 11:11 order to test the new media library number of items computer property. 11:16 And that's the second challenge, see you in just a bit. 11:21
You need to sign up for Treehouse in order to download course files.Sign up