Testing for a Specific Type6:04 with James Churchill
In this video, we'll review the solution to the second challenge and introduce our third challenge—adding a helper method that'll identify a media type item's specific subtype.
In the Program.cs file, create a static method named
- Define a parameter named
itemthat's of type
- Define a parameter named
DetectMediaType()method body, use a series of conditional statements and the C#
isoperator to identity which particular media type subclass (i.e. Album, Book, Movie, etc.) the
itemparameter is an instance of.
- Once the media type item's subclass type has been determined, output a message to the console that identifies that type.
- For example, if the
itemparameter is of type "Album" and its
Titlefield is set to the value "Yellow Submarine", write the message "Yellow Submarine is an album!" to the console.
To test your changes, pass each media type subclass instance into a call to the
For more information about the C#
is operator see https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is.
If you get stuck on any of the following topics or simply need a refresher, click on a topic in list below to view the associated video in the C# Objects course.
How did you do? 0:00 Let's take a look at my solution. 0:01 I started with moving the loan and return methods and the Loanee and 0:04 OnLoan fields from the Album subclass to the MediaType base class. 0:08 I cut the Loanee and OnLoan fields to the clipboard and open the MediaType.cs file. 0:13 Then paste it from the clipboard to add the loanee and 0:20 OnLoan fields to this class. 0:23 Then I did the same for the loan and return methods. 0:26 Cut to the clipboard, then switch back to the MediaType class. 0:35 And paste right below the constructor. 0:39 And Save. 0:45 Next, I added a GetDisplayText method to my book and movie MediaType subclasses. 0:47 To save some typing, 0:53 I copied the GetDisplayText method from the Album subclass. 0:54 And pasted it into the Book subclass, right below the constructor. 0:59 Then I changed Album here in this string literal to Book. 1:09 Title is okay, that's coming from the base class. 1:14 And I changed Artist to Author, cuz a book has an author. 1:18 And the rest of this code related to loaning can stay the same. 1:25 And now the Movie class. 1:28 Paste right below the constructor again. 1:32 This time I'll change Album here in the string literal to Movie of course. 1:38 Again Title can stay the same because that's coming from the base class. 1:44 And I'll change Artist to Director. 1:48 And again, all this code related to loaning, can stay the same. 1:53 To test my changes, I updated the Program.cs files main method. 1:57 First, I added Book and Movie MediaType subclass instances. 2:04 Var book = new Book. 2:11 For the book title, I'll use Moby-Dick. 2:19 And the author for Moby-Dick is Herman Melville. 2:24 Then a movie. 2:32 So, var movie = new Movie. 2:33 For the movie title I'll use Lawrence of Arabia, and 2:40 for the director, David Lean. 2:45 Then I added calls to the GetDisplayText methods on those instances 2:50 in order to write the information for those media types to the console. 2:54 I'll just copy this line of code to the clipboard and paste it twice. 2:59 Then I'll change album3 to book and album3 here to movie. 3:03 And in order to test, the loan and 3:12 return methods that we moved to the media type basic class. 3:14 I add calls to the loan and return methods on the book and movie instances. 3:18 So, book.Loan and movie.Loan, 3:22 then let's return those items. 3:26 book.Return And movie.Return. 3:30 And let's add additional GetDisplayText method calls. 3:41 That way we can see the media type information changing as we load and 3:48 return those items. 3:52 So book and movie. 3:54 Then I'll copy those two lines to the clipboard and paste them again down here. 3:59 And Save my file. 4:04 After I finished all of those changes, I compiled and ran the program. 4:06 VIew, Show Console. 4:11 And here's the output. 4:16 Here's my three albums, then the book and movie. 4:20 Then we can see the two albums that we're loaning out, 4:24 along with the book and movie. 4:27 Then we can see the two albums, the book and the movie after they've been returned. 4:29 Excellent work so far. 4:34 One of the benefits of having all of our media types subclasses inherit 4:35 from the same media type base class is that we can treat each media type item 4:39 as an instance of the media type class. 4:44 So, instead of having to refer to each item as an album, book or 4:47 movie subclass instance, we can refer to them all as media type class instances. 4:52 Because of this, we can pass any of these media types subclass instances 4:58 to a method that accepts a media type class instance. 5:03 Let's explore this idea further in our third challenge. 5:08 In the Program.cs file, create a static method named Detect MediaType. 5:11 That defines a parameter of type MediaType named item. 5:16 In the method body, use a series of conditional statements and 5:21 the C# is operator to identify which particular media type subclass 5:24 the item parameter is an instance of. 5:30 Once the subclass type has been determined, 5:32 output a message to the console that identifies the items type. 5:35 For example, if the item parameter is of type Album and 5:39 its title field is set to the value Yellow Submarine, write the message, 5:43 Yellow Submarine is an album, to the console. 5:48 To test your changes, pass each media type subclass instance 5:52 into a call to the DetectMediaType() method. 5:56 And that's the third challenge. 5:59 After the break, I'll show you my solution. 6:01
You need to sign up for Treehouse in order to download course files.Sign up