Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Creating an Array3:27 with James Churchill
In this video, we'll introduce our first challenge—creating an array for our Media Library program media type items.
This practice session assumes that you've completed stage 4 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 these three practice sessions that cover stages 1-3 of the C# Objects course.
If you haven't completed these practice sessions yet, you might consider doing that first before attempting this practice session.
Declare a variable for an array of type MediaType.
Initialize the array with the existing media type subclass instances.
- Feel free to add additional media type subclass instances!
Update the calls to the
Comment out any code past the calls to 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.
- Recall that an array is a numbered list of items
- Recall that an item's position in an array is known as its "index"
- Recall that an array's first item has an index of "0"
- Recall that when declaring a variable for an array you start with the data type followed by a set of brackets ("")
- Declare a variable for an array of strings
- Instantiate an array of strings
- Set an array item's value
- Get an array item's value
- Set the items in an array when declaring it
- Declare and instantiate an array of strings using the concise syntax
Hi there, this is James.
In this C# practice session, you'll practice encapsulation and
arrays using C#.
It reinforces what you learned in stage four of the C# objects course.
If you find this practice session too challenging to complete,
you might need to review that course, see the teachers notes for a link.
This practice session is the fourth 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 use C# to add, list, and search for items like
albums, books, movies, whatever you want to have cataloged in your media library.
In the previous practice sessions we created a class hierarchy for
our programs media types.
In this practice session, we'll see how we can use encapsulation and
arrays to improve the design of our program.
Go ahead and open your work space from the previous practice session.
Or you can open the work space that I 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.
If you open the Program.cs file, you'll see here, in the main method, where we're
instantiating instances of our media library programs, media type subclasses.
Three album subclass instances,
one book subclass instance, and a movie subclass instance.
These subclasses are defined in the Album.cs file,
the Book.cs file, and the Movie.cs file.
If we take a closer look at the Album class, we can see that it inherits from
the MediaType base class, which is defined in the MediaType.cs file.
The MediaType base class defines the attributes and
behaviors that are shared across all of our MediaType subclasses,
including the Title, Loanee, and OnLoan fields, and Loan and Return methods.
In the Program.cs file, instead of having to define a separate variable for
each MediaType subclass instance,
let's use an array to define a collection of MediaType items.
This change will make it easier to add new items, and it'll set us up for
enhancements that we'll make to our media library program
in future practice sessions.
For your first challenge, you'll declare a variable for an array of type MediaType
and initialize the array with your existing MediaType subclass instances.
Feel free to add additional MediaType subclass instances.
Once you've moved your MediaType subclass instances into an array,
you'll need to update the calls to the DetectMediaType method.
As the variables that are being passed into the method calls won't exist anymore.
And, finally, comment out any code
past the calls to the DetectMediaType method, and that's your first challenge.
Try your best to fully complete the challenge, but
don't worry if you get hung up on something.
Complete as much of it as you can, and in the next video I'll show you my solution.
Good luck and we'll see in you in the next video.
You need to sign up for Treehouse in order to download course files.Sign up