1 00:00:00,600 --> 00:00:04,630 At the end of the previous video, we were getting an error when attempting to call 2 00:00:04,630 --> 00:00:10,310 the GetDisplayText method on an element in our MediaType items array. 3 00:00:10,310 --> 00:00:15,362 Type Treehouse.MediaLibrary.MediaType does not contain a definition for 4 00:00:15,362 --> 00:00:19,724 a GetDisplayText, and no extension method GetDisplayText of type 5 00:00:19,724 --> 00:00:23,980 Treehouse.MediaLibrary.MediaType could be found. 6 00:00:23,980 --> 00:00:27,190 Luckily, this error tells us exactly what's happening. 7 00:00:27,190 --> 00:00:32,180 Because our array items, RF type, media type, and the GetDisplayText method 8 00:00:32,180 --> 00:00:36,950 isn't defined on the MediaType class, we can't call that method. 9 00:00:36,950 --> 00:00:40,500 We can work around this limitation using an approach similar 10 00:00:40,500 --> 00:00:43,400 to what the DetectMediaType method uses. 11 00:00:43,400 --> 00:00:45,600 Let's make a copy of that method. 12 00:00:45,600 --> 00:00:49,740 First, I'll select all of this code for the DetectMediaType method, 13 00:00:49,740 --> 00:00:53,472 copy it to the clipboard, and paste it right above that method. 14 00:00:57,565 --> 00:01:00,721 Then I'll change the method name to Display, and 15 00:01:00,721 --> 00:01:05,918 remove the code contained within each of the conditional statement code blocks. 16 00:01:05,918 --> 00:01:11,170 So here, here, and here. 17 00:01:11,170 --> 00:01:16,840 By using the C# is operator, we can detect the MediaTypeItem subclass type. 18 00:01:16,840 --> 00:01:20,150 Then we can use an explicit cast to convert the item 19 00:01:20,150 --> 00:01:23,930 from the MediaType type to its subclass type. 20 00:01:23,930 --> 00:01:28,844 So if (item is Album), 21 00:01:28,844 --> 00:01:35,630 then var album = (Album) item. 22 00:01:36,670 --> 00:01:39,290 This line of code explicitly casts 23 00:01:39,290 --> 00:01:43,710 the MediaType item parameter to the Album type. 24 00:01:43,710 --> 00:01:46,800 You have to be careful when using an explicit cast. 25 00:01:46,800 --> 00:01:49,220 If the type can't be successfully converted, 26 00:01:49,220 --> 00:01:51,050 you'll get a run time exception. 27 00:01:51,050 --> 00:01:55,399 We know that this explicit cast to the Album subtype is safe to do, though. 28 00:01:55,399 --> 00:02:01,076 Because we're using the is keyword to test of the item, which is of the base type, 29 00:02:01,076 --> 00:02:05,540 MediaType, is actually of the more specific subtype, Album. 30 00:02:07,610 --> 00:02:12,315 After casting the item to the Album type, we can then successfully 31 00:02:12,315 --> 00:02:17,286 call any method on that type, including the GetDisplayText method. 32 00:02:19,983 --> 00:02:24,530 And lets pass the return value from the GetDisplayText method call into 33 00:02:24,530 --> 00:02:26,890 a Console.WriteLine method call. 34 00:02:30,430 --> 00:02:33,884 If you want, you could remove the interim variable like this. 35 00:02:40,999 --> 00:02:44,560 Here's the item parameter being cast to Album. 36 00:02:44,560 --> 00:02:47,180 Then we surround all of that in parentheses in 37 00:02:47,180 --> 00:02:50,080 order to call the GetDisplayText method. 38 00:02:50,080 --> 00:02:54,280 Declaring a variable is useful when you need to do more than one operation 39 00:02:54,280 --> 00:02:55,640 with the object. 40 00:02:55,640 --> 00:02:59,199 Otherwise I typically just do the explicit cast in line. 41 00:02:59,199 --> 00:03:02,350 Sometimes it's a little bit more difficult to read, but 42 00:03:02,350 --> 00:03:05,560 generally I like the conciseness of the code. 43 00:03:05,560 --> 00:03:10,273 Let's copy and paste this line here and here. 44 00:03:12,234 --> 00:03:18,910 And change Album to Book, And Album here to Movie. 45 00:03:20,760 --> 00:03:23,690 This completes our method implementation. 46 00:03:23,690 --> 00:03:26,082 Now we can call our new display method. 47 00:03:28,607 --> 00:03:31,892 Let's do that right after our calls to DetectMediaType. 48 00:03:31,892 --> 00:03:38,975 So Display, then, (items[0]). 49 00:03:38,975 --> 00:03:42,030 I'll copy that to the clipboard and paste four times. 50 00:03:44,780 --> 00:03:49,500 Change this index to 1, then 2, then 3, then 4. 51 00:03:50,920 --> 00:03:54,765 Now, let's compile and run our program, well, after saving the file. 52 00:03:54,765 --> 00:03:58,520 View > Show Console, 53 00:04:02,016 --> 00:04:04,220 And we get the expected output. 54 00:04:05,330 --> 00:04:09,940 This solution works fine, but unfortunately, it's not an ideal solution. 55 00:04:09,940 --> 00:04:11,370 In a future course, 56 00:04:11,370 --> 00:04:15,900 you'll learn how another feature of object oriented programming, abstract classes and 57 00:04:15,900 --> 00:04:20,050 methods, can provide an alternative solution to this problem. 58 00:04:20,050 --> 00:04:22,610 See the teacher's notes for more information. 59 00:04:22,610 --> 00:04:26,340 Now, let's look at another feature of object oriented programming, 60 00:04:26,340 --> 00:04:28,010 encapsulation. 61 00:04:28,010 --> 00:04:31,170 Aside from our MediaType classes, the implementation for 62 00:04:31,170 --> 00:04:35,590 our media library lives entirely here in the main method. 63 00:04:35,590 --> 00:04:37,970 While this approach has worked fine up to this point, 64 00:04:37,970 --> 00:04:42,600 we want to make it as easy as possible to write code for our media library. 65 00:04:42,600 --> 00:04:46,720 Also, anyone using our media library shouldn't have to know that we're using 66 00:04:46,720 --> 00:04:49,980 an array for our collection of MediaType items. 67 00:04:49,980 --> 00:04:53,250 Let's add a media library class to our program and 68 00:04:53,250 --> 00:04:56,880 move our MediaType items array into that class. 69 00:04:56,880 --> 00:05:02,170 Doing that will hide, or encapsulate, our media library implementation. 70 00:05:02,170 --> 00:05:03,820 For your second challenge, 71 00:05:03,820 --> 00:05:07,990 you'll encapsulate the array of MediaType items inside of a class. 72 00:05:07,990 --> 00:05:12,030 To do that, you'll create a class named MediaLibrary. 73 00:05:12,030 --> 00:05:13,920 Within the MediaLibrary class, 74 00:05:13,920 --> 00:05:20,360 you'll define a private array field of type MediaType named _items. 75 00:05:20,360 --> 00:05:23,820 Then you'll initialize the array via a constructor method 76 00:05:23,820 --> 00:05:26,710 that accepts an array of media type items. 77 00:05:26,710 --> 00:05:32,260 And, you'll add a method named GetItemAt that accepts an index value and 78 00:05:32,260 --> 00:05:35,988 returns the media type item at the provided index. 79 00:05:35,988 --> 00:05:38,720 In the Program.cs file Main method, 80 00:05:38,720 --> 00:05:42,460 you'll instantiate an instance of the MediaLibrary class, 81 00:05:42,460 --> 00:05:46,520 passing into the constructor your existing collection of items. 82 00:05:46,520 --> 00:05:50,690 Then you'll update the rest of the code to use the GetItemAt method 83 00:05:50,690 --> 00:05:53,300 to retrieve items from the media library. 84 00:05:53,300 --> 00:05:55,650 And that's your second challenge. 85 00:05:55,650 --> 00:05:57,650 After the break, I'll show you my solution.