1 00:00:00,380 --> 00:00:01,320 How did you do? 2 00:00:01,320 --> 00:00:03,070 Let's take a look at my solution. 3 00:00:03,070 --> 00:00:04,840 In the MediaLibrary class, 4 00:00:04,840 --> 00:00:09,750 I updated the GetItemAt method to gracefully handle a bad index. 5 00:00:09,750 --> 00:00:12,630 To do that, I used the array's linked property 6 00:00:12,630 --> 00:00:16,954 to determine if an index was available before attempting to access it. 7 00:00:16,954 --> 00:00:23,760 If ( then index, and then what we want to do is check to see if the index is less 8 00:00:24,900 --> 00:00:30,034 than the length of the array, so items.Length). 9 00:00:33,289 --> 00:00:37,663 Then, we can cut and paste this line of code inside of our if statement. 10 00:00:39,386 --> 00:00:43,855 We still need to handle the case when the index is not less than the _items.Length 11 00:00:43,855 --> 00:00:44,970 property. 12 00:00:44,970 --> 00:00:50,475 So else, set of {}, and then here, let's just return null. 13 00:00:53,060 --> 00:00:57,140 This code as it's written will work fine, but you could optionally use 14 00:00:57,140 --> 00:01:00,890 the ternary if operator if you want to make a little more concise. 15 00:01:00,890 --> 00:01:02,090 Let's take a look at that. 16 00:01:04,498 --> 00:01:11,706 We'll start with the return keyword followed 17 00:01:11,706 --> 00:01:17,802 by index < _items.Length then a ?, 18 00:01:17,802 --> 00:01:23,727 then _items(index) a : and null. 19 00:01:26,055 --> 00:01:27,760 Let's break this down. 20 00:01:27,760 --> 00:01:29,640 This is our condition, so 21 00:01:29,640 --> 00:01:34,900 if this condition evaluates the true, this is the value that's returned. 22 00:01:34,900 --> 00:01:36,916 Otherwise, this is the value that's returned. 23 00:01:38,600 --> 00:01:42,310 Sometimes adding parenthesis around the condition makes this code a little bit 24 00:01:42,310 --> 00:01:43,093 easier to read. 25 00:01:44,418 --> 00:01:49,205 It's worth noting that some developers like the clarity of this approach, 26 00:01:49,205 --> 00:01:53,030 while others like the conciseness of this approach. 27 00:01:53,030 --> 00:01:54,880 Either way, it's fine to use. 28 00:01:54,880 --> 00:01:57,990 So use the one that you or your team prefers. 29 00:01:57,990 --> 00:02:02,075 Then, over in the Program.cs file, I updated the display and 30 00:02:02,075 --> 00:02:06,486 DetectedMediaType methods to handle a null item parameter value. 31 00:02:10,320 --> 00:02:11,280 First, the Display method. 32 00:02:13,205 --> 00:02:17,480 Right here at the beginning of the method, I added an if statement. 33 00:02:17,480 --> 00:02:24,423 So if item, Is null, Then just return. 34 00:02:25,752 --> 00:02:29,815 This has the effect of short-circuiting the method when the item parameter 35 00:02:29,815 --> 00:02:30,800 is null. 36 00:02:30,800 --> 00:02:34,813 I'll select and copy this code, scroll down to the DetectMediaType method, and 37 00:02:34,813 --> 00:02:37,907 paste it right at the beginning of the method implementation. 38 00:02:40,410 --> 00:02:45,608 And lastly, I saved, compiled, and ran my program in order to test my changes. 39 00:02:47,979 --> 00:02:53,450 And now, a call to the GetItemAt method, with a non-existent index. 40 00:02:53,450 --> 00:02:56,070 For a bad index, we turn snull, 41 00:02:56,070 --> 00:03:01,350 which is in turn ignored by the display and DetectMediaType methods. 42 00:03:01,350 --> 00:03:05,944 It's worth pointing out that this solution helps to fix the problem with non-existing 43 00:03:05,944 --> 00:03:06,920 array indexes. 44 00:03:06,920 --> 00:03:10,646 But, it doesn't provide any feedback as to what happened during the execution of 45 00:03:10,646 --> 00:03:11,294 the program. 46 00:03:14,352 --> 00:03:19,200 One possibility, would be to simply write a message to the console when a bad 47 00:03:19,200 --> 00:03:22,050 index is passed to GetItemAt method. 48 00:03:22,050 --> 00:03:25,360 To do this, I'm gonna switch back to the less concise version of this code. 49 00:03:28,116 --> 00:03:34,739 And here in the else statement, I write System.Console.WriteLine, 50 00:03:37,269 --> 00:03:39,000 And then the message. 51 00:03:40,472 --> 00:03:47,290 An element at index, And then the index parameter value. 52 00:03:51,519 --> 00:03:55,705 Doesn't exist in the media library. 53 00:03:57,970 --> 00:04:00,660 Save the file, and now, when we compile and 54 00:04:00,660 --> 00:04:06,680 run the program, We see an appropriate error message. 55 00:04:06,680 --> 00:04:11,220 An element at index 5 doesn't exist in the media library. 56 00:04:11,220 --> 00:04:13,650 Nice job completing this practice session. 57 00:04:13,650 --> 00:04:16,350 Be sure to check out the next session in this series 58 00:04:16,350 --> 00:04:20,710 after you've completed the fifth stage in the C# Objects Course. 59 00:04:20,710 --> 00:04:23,190 Thanks for practicing with me, and we'll see you next time.