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
[MUSIC] 0:00 Hi there, this is James. 0:04 In this C# practice session, you'll practice encapsulation and 0:06 arrays using C#. 0:10 It reinforces what you learned in stage four of the C# objects course. 0:12 If you find this practice session too challenging to complete, 0:17 you might need to review that course, see the teachers notes for a link. 0:20 This practice session is the fourth in a series of sessions, 0:25 where you'll build out a media library console application, 0:29 step-by-step you'll add features to the program. 0:32 Eventually, you'll be able to use C# to add, list, and search for items like 0:36 albums, books, movies, whatever you want to have cataloged in your media library. 0:41 In the previous practice sessions we created a class hierarchy for 0:47 our programs media types. 0:51 In this practice session, we'll see how we can use encapsulation and 0:53 arrays to improve the design of our program. 0:57 Go ahead and open your work space from the previous practice session. 1:00 Or you can open the work space that I attached to this video. 1:04 If you want, you can download the project files in order to use an external editor 1:09 or IDE like Visual Studio. 1:13 If you open the Program.cs file, you'll see here, in the main method, where we're 1:16 instantiating instances of our media library programs, media type subclasses. 1:22 Three album subclass instances, 1:27 one book subclass instance, and a movie subclass instance. 1:31 These subclasses are defined in the Album.cs file, 1:37 the Book.cs file, and the Movie.cs file. 1:42 If we take a closer look at the Album class, we can see that it inherits from 1:47 the MediaType base class, which is defined in the MediaType.cs file. 1:51 The MediaType base class defines the attributes and 1:58 behaviors that are shared across all of our MediaType subclasses, 2:02 including the Title, Loanee, and OnLoan fields, and Loan and Return methods. 2:07 In the Program.cs file, instead of having to define a separate variable for 2:18 each MediaType subclass instance, 2:23 let's use an array to define a collection of MediaType items. 2:25 This change will make it easier to add new items, and it'll set us up for 2:29 enhancements that we'll make to our media library program 2:34 in future practice sessions. 2:37 For your first challenge, you'll declare a variable for an array of type MediaType 2:39 and initialize the array with your existing MediaType subclass instances. 2:44 Feel free to add additional MediaType subclass instances. 2:49 Once you've moved your MediaType subclass instances into an array, 2:53 you'll need to update the calls to the DetectMediaType method. 2:57 As the variables that are being passed into the method calls won't exist anymore. 3:01 And, finally, comment out any code 3:06 past the calls to the DetectMediaType method, and that's your first challenge. 3:09 Try your best to fully complete the challenge, but 3:14 don't worry if you get hung up on something. 3:17 Complete as much of it as you can, and in the next video I'll show you my solution. 3:19 Good luck and we'll see in you in the next video. 3:25
You need to sign up for Treehouse in order to download course files.Sign up