Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Adding a Method2:57 with James Churchill
In this video, we'll introduce our first challenge—adding a method to a class in order to define the first behavior for our Media Library program media types.
This practice session assumes that you've completed stage 2 of the C# Objects course. If you haven't done that yet, go check it out, then come back to this workshop.
This practice session also builds upon the skills that are covered in the Practice Creating Classes in C# practice session that covers stage 1 of the C# Objects course. If you haven't completed this practice session yet, you might consider doing that first before attempting this practice session.
- Add a
GetDisplayText()method to your selected media type class. This method should return a string containing the display text for the media type item.
- Example: The
GetDisplayText()method for an Album class would return the text "Album: Yellow Submarine by The Beatles" if the
Titlefield was set to "Yellow Submarine" and the
Artistfield was set to "The Beatles".
- Example: The
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.
[MUSIC] 0:00 Hi there, this is James. 0:05 In this C# practice session, 0:06 you'll practice adding methods to classes using C#. 0:09 It reinforces what you learned in stage two of this C# objects course. 0:13 If you find this practice session too challenging to complete 0:17 you might need to review that course. 0:20 See the teacher's notes for a link. 0:22 This practice session is the second in a series of sessions 0:25 where you'll build out a media library console application. 0:28 Step by step you'll add features to the program. 0:32 Eventually you'll be able to C# to add, list and 0:36 search for items like albums, books and movies. 0:40 Whatever you want to have cataloged in your media library. 0:43 In the previous practice session, we defined the attributes for 0:47 our program's media type objects by adding fields to our classes. 0:50 In this practice session, we'll start the process of defining behaviors for 0:55 our media type objects by adding methods to one of our classes. 0:59 I'll be using my album media type class. 1:04 If your program doesn't contain an album class, 1:06 feel free to use any of your immediate type classes. 1:09 In a future practice session, we'll use class inheritance an aspect of 1:12 object oriented programming to extend our behaviors to each of our media types. 1:17 Go ahead and open your workspace from the previous practice session. 1:22 Or you can open the workspace that I've attached to this video. 1:26 If you want, you can download the project files in order to use an external editor 1:30 or IDE like Visual Studio. 1:35 In the Program.cs file, 1:37 we can see where I'm creating instances of my media type classes. 1:39 An album object, a book object, and a movie object. 1:44 Here I'm writing information for 1:50 each media type object to the console using the console writeline method. 1:52 Instead of accessing the media type field values and 1:57 using string concatenation to create the text to write to the console, let's 2:00 add a method to the class that we can call to get the media types display text. 2:05 For your first challenge, 2:12 you'll add a GetDisplayText() method to your selected media type class. 2:14 This method will return a string containing the display text for 2:19 the media type item. 2:22 For example, the GetDisplayText() method for my Album class would return the text. 2:25 Album: Yellow Submarine by the Beatles, if the title 2:30 field was set to Yellow Submarine and the artist field was set to the Beatles. 2:35 Feel free to use whatever formatting and 2:40 fields that are available on your selected media type class. 2:42 And that's your first challenge If you 2:46 aren't able to fully complete this challenge, don't worry. 2:49 I'll show you my solution in the next video. 2:52 Good luck, and we'll see you in a bit. 2:55
You need to sign up for Treehouse in order to download course files.Sign up