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.
Hi there, this is James.
In this C# practice session,
you'll practice adding methods to classes using C#.
It reinforces what you learned in stage two of this C# objects course.
If you find this practice session too challenging to complete
you might need to review that course.
See the teacher's notes for a link.
This practice session is the second in a series of sessions
where you'll build out a media library console application.
Step by step you'll add features to the program.
Eventually you'll be able to C# to add, list and
search for items like albums, books and movies.
Whatever you want to have cataloged in your media library.
In the previous practice session, we defined the attributes for
our program's media type objects by adding fields to our classes.
In this practice session, we'll start the process of defining behaviors for
our media type objects by adding methods to one of our classes.
I'll be using my album media type class.
If your program doesn't contain an album class,
feel free to use any of your immediate type classes.
In a future practice session, we'll use class inheritance an aspect of
object oriented programming to extend our behaviors to each of our media types.
Go ahead and open your workspace from the previous practice session.
Or you can open the workspace that I've attached to this video.
If you want, you can download the project files in order to use an external editor
or IDE like Visual Studio.
In the Program.cs file,
we can see where I'm creating instances of my media type classes.
An album object, a book object, and a movie object.
Here I'm writing information for
each media type object to the console using the console writeline method.
Instead of accessing the media type field values and
using string concatenation to create the text to write to the console, let's
add a method to the class that we can call to get the media types display text.
For your first challenge,
you'll add a GetDisplayText() method to your selected media type class.
This method will return a string containing the display text for
the media type item.
For example, the GetDisplayText() method for my Album class would return the text.
Album: Yellow Submarine by the Beatles, if the title
field was set to Yellow Submarine and the artist field was set to the Beatles.
Feel free to use whatever formatting and
fields that are available on your selected media type class.
And that's your first challenge If you
aren't able to fully complete this challenge, don't worry.
I'll show you my solution in the next video.
Good luck, and we'll see you in a bit.
You need to sign up for Treehouse in order to download course files.Sign up