Reviewing the First Challenge6:01 with James Churchill
In this video, we'll review the solution to the first challenge and take a look at an issue when attempting to the call the `GetDisplayText()` method on one of the items in the MediaType items array.
Welcome back, how did you do? 0:00 Don't worry if you weren't able to complete the entire challenge. 0:02 After reviewing my solution, try again to complete the parts 0:06 you weren't able to finish, let's walk through my solution. 0:09 In the program.cs, file I started with declaring a variable for 0:12 an array of type, MediaType. 0:17 So, first the type, MediaType, 0:19 followed by a pair of brackets, then the name of the variable, items. 0:23 Then I initialize the array, using my existing MediaType subclass instances. 0:28 There are many ways to initialize an array in C-Sharp. 0:33 We can use the new keyword followed by the type, and 0:36 how long we want to make the array. 0:40 So = new MediaType, and a pair of brackets. 0:41 Then inside of the the brackets, 0:49 the number of elements we want to have in the array. 0:50 So 5, because we have three albums, a book, and a movie. 0:52 Then we'd set each elements value in the array like this. 0:59 Items, a pair of brackets, and then the index of the element we want to set. 1:03 The first element is the index 0. 1:09 Then =, and then we set it to the value. 1:12 In this case I'm gonna cut, and paste this code right here. 1:16 Because I don't want the first element to be an instance of this new album. 1:20 Then, we'd repeat that code once for each element in the array. 1:26 We can also set each element's value by including a set of curly braces 1:30 after instantiating the array. 1:34 Then, inside of the curly braces, we provide a value for 1:36 each element in the array. 1:40 There's the value for the first element, For 1:45 the second element, For the third element. 1:50 For the fourth element, and one more to go, the movie. 2:03 Then I can do a little clean-up and remove this code. 2:12 We can also shorten this a bit. 2:17 The C-Sharp compiler can infer the link to the array 2:19 by looking at how many items we've included here between the curly braces. 2:23 So, we can remove this literal five 2:27 between the brackets following the type of the array. 2:30 In fact, we could also go further and remove the new keyword and the array type. 2:34 This is possible because we're explicitly defining the variable's type here. 2:42 If you were to replace this with the var keyword, 2:48 then you need to add back new MediaType brackets. 2:52 This is necessary as the C-Sharp compiler is unable to determine what type to 3:00 use for the array, now that we've switched to using the var keyword. 3:04 Should the array have been an array of albums, an array of books, or 3:09 an array of movies? 3:14 Adding the type here clarifies that. 3:16 I ended up explicitly defining the variables type, and 3:25 using the most concise syntax that I could use for the initialization of the array. 3:28 But feel free to use any of the available options. 3:33 Now that I've declared and initialized my array, 3:39 I updated the calls to the DetectMediaType method, items , 3:43 1, 2, 3, and 4. 3:54 And I commented out all of the code below those method calls up into the end of 4:05 the code block. 4:10 And lastly, I saved the file, compiled and ran my program. 4:15 And here's the expected output. 4:23 Here are the three albums being identified as albums, 4:26 the book being identified as the book, and the movie being identified as the movie. 4:29 Updating our program to use an array for the collection of MediaType items, 4:34 helps to make our code a bit more elegant in its approach and design. 4:39 But making this change also introduces some challenges. 4:43 For example, it's no longer possible to directly call 4:47 the getDisplayText method on an item in the MediaType items array. 4:51 Let's try it and see what happens. 5:01 Console.WriteLine, Items and the the first element. 5:03 So, index 0.GetDisplayText, 5:10 save and compile and run our program. 5:20 And we receive the following error. 5:26 Type Treehouse.MediaLibrary.MediaType, 5:28 does not contain a definition for a get display text. 5:33 And no extension method get display text of,Treehouse.MediaLibrary.MediaType, 5:36 could be find. 5:42 Are you missing an assembly reference? 5:43 The error tells us exactly what's happening. 5:45 Because our array elements are of type MediaType, and the GetDisplayText 5:48 method isn't defined on the MediaType class, we can't call that method. 5:53 In the next video, we'll see how we can work around this limitation. 5:58
You need to sign up for Treehouse in order to download course files.Sign up