Creating a Class to Encapsulate an Array5:58 with James Churchill
In this video, we'll see how to explicitly cast a base type to a subtype and we'll introduce our second challenge—creating a class in order to encapsulate our media type items array.
Encapsulate the array of media type items inside of a class.
- Create a class named "MediaLibrary".
In the MediaLibrary class...
- Define a private array field of type MediaType named
- Initialize the array via a constructor method that accepts an array of media type items.
- Add a method named
GetItemAt()that accepts an index value and returns the media type item at the provided index.
- Define a private array field of type MediaType named
In the Program.cs file
- Instantiate an instance of the MediaLibrary class.
- Pass into the constructor the existing collection of items.
- Update the rest of the code to use the
GetItemAt()method to retrieve items from the media library.
For more information about abstract classes and class members in C# see the Intermediate C# course in the Treehouse library.
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.
At the end of the previous video, we were getting an error when attempting to call 0:00 the GetDisplayText method on an element in our MediaType items array. 0:04 Type Treehouse.MediaLibrary.MediaType does not contain a definition for 0:10 a GetDisplayText, and no extension method GetDisplayText of type 0:15 Treehouse.MediaLibrary.MediaType could be found. 0:19 Luckily, this error tells us exactly what's happening. 0:23 Because our array items, RF type, media type, and the GetDisplayText method 0:27 isn't defined on the MediaType class, we can't call that method. 0:32 We can work around this limitation using an approach similar 0:36 to what the DetectMediaType method uses. 0:40 Let's make a copy of that method. 0:43 First, I'll select all of this code for the DetectMediaType method, 0:45 copy it to the clipboard, and paste it right above that method. 0:49 Then I'll change the method name to Display, and 0:57 remove the code contained within each of the conditional statement code blocks. 1:00 So here, here, and here. 1:05 By using the C# is operator, we can detect the MediaTypeItem subclass type. 1:11 Then we can use an explicit cast to convert the item 1:16 from the MediaType type to its subclass type. 1:20 So if (item is Album), 1:23 then var album = (Album) item. 1:28 This line of code explicitly casts 1:36 the MediaType item parameter to the Album type. 1:39 You have to be careful when using an explicit cast. 1:43 If the type can't be successfully converted, 1:46 you'll get a run time exception. 1:49 We know that this explicit cast to the Album subtype is safe to do, though. 1:51 Because we're using the is keyword to test of the item, which is of the base type, 1:55 MediaType, is actually of the more specific subtype, Album. 2:01 After casting the item to the Album type, we can then successfully 2:07 call any method on that type, including the GetDisplayText method. 2:12 And lets pass the return value from the GetDisplayText method call into 2:19 a Console.WriteLine method call. 2:24 If you want, you could remove the interim variable like this. 2:30 Here's the item parameter being cast to Album. 2:40 Then we surround all of that in parentheses in 2:44 order to call the GetDisplayText method. 2:47 Declaring a variable is useful when you need to do more than one operation 2:50 with the object. 2:54 Otherwise I typically just do the explicit cast in line. 2:55 Sometimes it's a little bit more difficult to read, but 2:59 generally I like the conciseness of the code. 3:02 Let's copy and paste this line here and here. 3:05 And change Album to Book, And Album here to Movie. 3:12 This completes our method implementation. 3:20 Now we can call our new display method. 3:23 Let's do that right after our calls to DetectMediaType. 3:28 So Display, then, (items). 3:31 I'll copy that to the clipboard and paste four times. 3:38 Change this index to 1, then 2, then 3, then 4. 3:44 Now, let's compile and run our program, well, after saving the file. 3:50 View > Show Console, 3:54 And we get the expected output. 4:02 This solution works fine, but unfortunately, it's not an ideal solution. 4:05 In a future course, 4:09 you'll learn how another feature of object oriented programming, abstract classes and 4:11 methods, can provide an alternative solution to this problem. 4:15 See the teacher's notes for more information. 4:20 Now, let's look at another feature of object oriented programming, 4:22 encapsulation. 4:26 Aside from our MediaType classes, the implementation for 4:28 our media library lives entirely here in the main method. 4:31 While this approach has worked fine up to this point, 4:35 we want to make it as easy as possible to write code for our media library. 4:37 Also, anyone using our media library shouldn't have to know that we're using 4:42 an array for our collection of MediaType items. 4:46 Let's add a media library class to our program and 4:49 move our MediaType items array into that class. 4:53 Doing that will hide, or encapsulate, our media library implementation. 4:56 For your second challenge, 5:02 you'll encapsulate the array of MediaType items inside of a class. 5:03 To do that, you'll create a class named MediaLibrary. 5:07 Within the MediaLibrary class, 5:12 you'll define a private array field of type MediaType named _items. 5:13 Then you'll initialize the array via a constructor method 5:20 that accepts an array of media type items. 5:23 And, you'll add a method named GetItemAt that accepts an index value and 5:26 returns the media type item at the provided index. 5:32 In the Program.cs file Main method, 5:35 you'll instantiate an instance of the MediaLibrary class, 5:38 passing into the constructor your existing collection of items. 5:42 Then you'll update the rest of the code to use the GetItemAt method 5:46 to retrieve items from the media library. 5:50 And that's your second challenge. 5:53 After the break, I'll show you my solution. 5:55
You need to sign up for Treehouse in order to download course files.Sign up