1 00:00:00,460 --> 00:00:01,537 How did you do? 2 00:00:01,537 --> 00:00:03,596 Let's take a look at my solution. 3 00:00:03,596 --> 00:00:09,953 In the MediaLibrary class, I stubbed out a method an item named find Item. 4 00:00:16,675 --> 00:00:21,066 I did that right above the DisplayItems method. 5 00:00:21,066 --> 00:00:26,168 Public for the access modifier and MediaType for the return type. 6 00:00:26,168 --> 00:00:30,389 Then the name of the method FindItem followed by a set of parentheses, 7 00:00:31,720 --> 00:00:35,705 And I defined a parameter of type string named criteria. 8 00:00:39,405 --> 00:00:43,528 Then, within the method body, I start it with defining the variable for 9 00:00:43,528 --> 00:00:46,579 the MediaType item that will return from the method. 10 00:00:48,518 --> 00:00:52,379 MediaType, itemToReturn, 11 00:00:54,330 --> 00:00:57,354 And we'll set it to null. 12 00:00:57,354 --> 00:01:03,037 And as a habit, I try to remember to add the code right now to return the variable. 13 00:01:03,037 --> 00:01:07,995 That way, I won't forget to do it later. 14 00:01:07,995 --> 00:01:12,248 Then in between declaring the itemToReturn variable and returning it, 15 00:01:12,248 --> 00:01:13,718 I added my for each loop. 16 00:01:15,663 --> 00:01:20,986 For each, then a set of parentheses and a set of curly braces. 17 00:01:22,272 --> 00:01:27,976 Then within the set of parentheses after the foreach 18 00:01:27,976 --> 00:01:32,779 keyword, I added (var item in _items). 19 00:01:32,779 --> 00:01:36,960 Remember that the foreach loop will loop each item in the array. 20 00:01:36,960 --> 00:01:42,566 We just need to declare a variable to use for the item references here, 21 00:01:42,566 --> 00:01:47,233 and provide the collection that we want to loop over here. 22 00:01:47,233 --> 00:01:50,343 Within the loop I added an if statement. 23 00:01:55,415 --> 00:02:00,549 And for the condition I called the contains method on 24 00:02:00,549 --> 00:02:05,801 the item's title property, passing in the criteria 25 00:02:05,801 --> 00:02:10,717 parameter, item.Title.Contains criteria. 26 00:02:12,759 --> 00:02:17,054 The string contains method will return true if the provided string 27 00:02:17,054 --> 00:02:21,363 is contained within the string that you’ve called the method on. 28 00:02:21,363 --> 00:02:26,285 One downside of this approach is that the contains method is case sensitive. 29 00:02:26,285 --> 00:02:30,843 This means that a string that starts with a capital letter won’t match a string that 30 00:02:30,843 --> 00:02:32,677 starts with a lower case letter. 31 00:02:32,677 --> 00:02:36,982 This behavior might not be what consumers of our class would expect. 32 00:02:36,982 --> 00:02:41,447 To work around this issue, we can force both of the strings in our condition to 33 00:02:41,447 --> 00:02:44,413 lower case letters by calling the ToLower method. 34 00:02:46,617 --> 00:02:52,623 So item.ToLower, And 35 00:02:52,623 --> 00:02:57,707 then on criteria.ToLower. 36 00:02:57,707 --> 00:03:02,283 Now it doesn't matter that the contains method is case sensitive because both 37 00:03:02,283 --> 00:03:06,950 strings will be converted to lower case letters before the comparison is done. 38 00:03:09,037 --> 00:03:14,543 Then within the if statement I assigned the itemToReturn variable to the item loop 39 00:03:14,543 --> 00:03:21,081 variable And 40 00:03:21,081 --> 00:03:26,053 I added a break statement to exit the loop as soon as we found a matching item. 41 00:03:32,630 --> 00:03:37,015 And that completed my find item method implementation. 42 00:03:37,015 --> 00:03:39,767 Then in the program.cs file, 43 00:03:43,867 --> 00:03:48,104 I started by commenting out all of the code below the instantiation of 44 00:03:48,104 --> 00:03:49,792 the MediaLibrary object. 45 00:03:57,488 --> 00:04:00,935 And then I added a call to the find item method. 46 00:04:00,935 --> 00:04:06,920 Var item = mediaLibrary.FindItem and 47 00:04:06,920 --> 00:04:09,736 for the criteria, 48 00:04:09,736 --> 00:04:15,373 I passed in arabia with a lowercase a. 49 00:04:17,473 --> 00:04:22,048 From there, I needed to check to see if an item was found or not, so 50 00:04:22,048 --> 00:04:24,559 I stubbed out an if else statement. 51 00:04:32,544 --> 00:04:35,939 For the condition I checked if the item wasn't null. 52 00:04:35,939 --> 00:04:40,621 So, (item != null). 53 00:04:40,621 --> 00:04:45,295 Checking if the item is null or not works because I'm returning null 54 00:04:45,295 --> 00:04:49,313 from the find item method if a matching item isn't found. 55 00:04:49,313 --> 00:04:51,054 Within the if statement, 56 00:04:51,054 --> 00:04:55,500 I added a call to the mediaLibrary.DisplayItems static method. 57 00:04:57,705 --> 00:05:02,349 So that's MediaLibrary with a capital M because we're calling is static method. 58 00:05:02,349 --> 00:05:09,335 Then .DdisplayItem, and I'll pass in the item to display. 59 00:05:13,235 --> 00:05:18,178 And within the else statement I added a call to the Console.writeLine method, 60 00:05:18,178 --> 00:05:20,554 passing in the string, item not found 61 00:05:30,390 --> 00:05:33,799 And lastly, I saved my file, I compiled and 62 00:05:33,799 --> 00:05:37,220 I ran my program in order to test my changes. 63 00:05:55,536 --> 00:06:00,809 And I've received a compilation error Type 'Treehouse.MediaLibrary.MediaType' does 64 00:06:00,809 --> 00:06:05,343 not contain a definition for 'Tolower' and no extension method 'Tolower' of 65 00:06:05,343 --> 00:06:09,099 type 'Treehouse.MediaLibrary.MediaType' could be found. 66 00:06:09,099 --> 00:06:11,799 Let's take a look at that code and see what I did. 67 00:06:16,559 --> 00:06:17,524 Here it is. 68 00:06:17,524 --> 00:06:23,563 I'm trying to call ToLower on item instead of calling ToLower on the title property. 69 00:06:23,563 --> 00:06:25,746 That's an easy mistake to make. 70 00:06:30,151 --> 00:06:35,007 So now it's item.Title property. 71 00:06:35,007 --> 00:06:40,095 Which is a string, which we then can call ToLower on. 72 00:06:40,095 --> 00:06:43,994 I'll save, and try to compile my program again. 73 00:06:48,823 --> 00:06:54,342 And now we can see that an item was found whose title contains the string, Arabia. 74 00:06:58,956 --> 00:07:03,491 Now let's try changing our search criteria to something that won't be found in our 75 00:07:03,491 --> 00:07:04,411 media library. 76 00:07:07,936 --> 00:07:10,548 How about the word, toy? 77 00:07:17,906 --> 00:07:20,857 Then let's compile and run our program again. 78 00:07:22,984 --> 00:07:25,623 And now we can see that an item wasn't found. 79 00:07:27,781 --> 00:07:29,285 Great job. 80 00:07:29,285 --> 00:07:32,810 Finding an element in an array is such a common thing to do. 81 00:07:32,810 --> 00:07:37,471 The .NET framework array type actually provides a static method for 82 00:07:37,471 --> 00:07:40,133 doing exactly that, the find method. 83 00:07:40,133 --> 00:07:42,877 See the teachers notes for more information. 84 00:07:42,877 --> 00:07:45,716 When learning the language, library or framework, 85 00:07:45,716 --> 00:07:48,497 it's not unusual to overlook a built-in feature. 86 00:07:48,497 --> 00:07:52,427 This is a good remainder to always consult the documentation. 87 00:07:52,427 --> 00:07:56,841 Getting into the habit of doing that will save you time by not building or 88 00:07:56,841 --> 00:07:59,723 implementing features that you don´t need. 89 00:07:59,723 --> 00:08:02,432 Nice job completing this practice session. 90 00:08:02,432 --> 00:08:05,631 Thanks for practicing with me and we´ll see you next time.