Wrapping Up8:05 with James Churchill
In this video, we'll review the solution to the second and final challenge.
Finding an Element in an Array
As mentioned in the video, finding an element in an array is such a common thing to do, the .NET Framework Array type provides a static method for doing exactly that—the
Find() method. Here's the documentation for the
How did you do? 0:00 Let's take a look at my solution. 0:01 In the MediaLibrary class, I stubbed out a method an item named find Item. 0:03 I did that right above the DisplayItems method. 0:16 Public for the access modifier and MediaType for the return type. 0:21 Then the name of the method FindItem followed by a set of parentheses, 0:26 And I defined a parameter of type string named criteria. 0:31 Then, within the method body, I start it with defining the variable for 0:39 the MediaType item that will return from the method. 0:43 MediaType, itemToReturn, 0:48 And we'll set it to null. 0:54 And as a habit, I try to remember to add the code right now to return the variable. 0:57 That way, I won't forget to do it later. 1:03 Then in between declaring the itemToReturn variable and returning it, 1:07 I added my for each loop. 1:12 For each, then a set of parentheses and a set of curly braces. 1:15 Then within the set of parentheses after the foreach 1:22 keyword, I added (var item in _items). 1:27 Remember that the foreach loop will loop each item in the array. 1:32 We just need to declare a variable to use for the item references here, 1:36 and provide the collection that we want to loop over here. 1:42 Within the loop I added an if statement. 1:47 And for the condition I called the contains method on 1:55 the item's title property, passing in the criteria 2:00 parameter, item.Title.Contains criteria. 2:05 The string contains method will return true if the provided string 2:12 is contained within the string that you’ve called the method on. 2:17 One downside of this approach is that the contains method is case sensitive. 2:21 This means that a string that starts with a capital letter won’t match a string that 2:26 starts with a lower case letter. 2:30 This behavior might not be what consumers of our class would expect. 2:32 To work around this issue, we can force both of the strings in our condition to 2:36 lower case letters by calling the ToLower method. 2:41 So item.ToLower, And 2:46 then on criteria.ToLower. 2:52 Now it doesn't matter that the contains method is case sensitive because both 2:57 strings will be converted to lower case letters before the comparison is done. 3:02 Then within the if statement I assigned the itemToReturn variable to the item loop 3:09 variable And 3:14 I added a break statement to exit the loop as soon as we found a matching item. 3:21 And that completed my find item method implementation. 3:32 Then in the program.cs file, 3:37 I started by commenting out all of the code below the instantiation of 3:43 the MediaLibrary object. 3:48 And then I added a call to the find item method. 3:57 Var item = mediaLibrary.FindItem and 4:00 for the criteria, 4:06 I passed in arabia with a lowercase a. 4:09 From there, I needed to check to see if an item was found or not, so 4:17 I stubbed out an if else statement. 4:22 For the condition I checked if the item wasn't null. 4:32 So, (item != null). 4:35 Checking if the item is null or not works because I'm returning null 4:40 from the find item method if a matching item isn't found. 4:45 Within the if statement, 4:49 I added a call to the mediaLibrary.DisplayItems static method. 4:51 So that's MediaLibrary with a capital M because we're calling is static method. 4:57 Then .DdisplayItem, and I'll pass in the item to display. 5:02 And within the else statement I added a call to the Console.writeLine method, 5:13 passing in the string, item not found 5:18 And lastly, I saved my file, I compiled and 5:30 I ran my program in order to test my changes. 5:33 And I've received a compilation error Type 'Treehouse.MediaLibrary.MediaType' does 5:55 not contain a definition for 'Tolower' and no extension method 'Tolower' of 6:00 type 'Treehouse.MediaLibrary.MediaType' could be found. 6:05 Let's take a look at that code and see what I did. 6:09 Here it is. 6:16 I'm trying to call ToLower on item instead of calling ToLower on the title property. 6:17 That's an easy mistake to make. 6:23 So now it's item.Title property. 6:30 Which is a string, which we then can call ToLower on. 6:35 I'll save, and try to compile my program again. 6:40 And now we can see that an item was found whose title contains the string, Arabia. 6:48 Now let's try changing our search criteria to something that won't be found in our 6:58 media library. 7:03 How about the word, toy? 7:07 Then let's compile and run our program again. 7:17 And now we can see that an item wasn't found. 7:22 Great job. 7:27 Finding an element in an array is such a common thing to do. 7:29 The .NET framework array type actually provides a static method for 7:32 doing exactly that, the find method. 7:37 See the teachers notes for more information. 7:40 When learning the language, library or framework, 7:42 it's not unusual to overlook a built-in feature. 7:45 This is a good remainder to always consult the documentation. 7:48 Getting into the habit of doing that will save you time by not building or 7:52 implementing features that you don´t need. 7:56 Nice job completing this practice session. 7:59 Thanks for practicing with me and we´ll see you next time. 8:02
You need to sign up for Treehouse in order to download course files.Sign up