Using a `for` Loop4:13 with James Churchill
In this video, we'll introduce our first challenge—using a `for` loop to loop through our Media Library items and output each item's information to the console.
This practice session assumes that you've completed stage 6 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 five practice sessions that cover stages 1-5 of the C# Objects course.
- Practice Creating Classes in C#
- Practice Methods in C#
- Practice Inheritance in C#
- Practice Encapsulation and Arrays in C#
- Practice Properties in C#
If you haven't completed these practice sessions yet, you might consider doing that first before attempting this practice session.
Add a method named
DisplayItems()to the MediaLibrary class.
- Use a
forloop to loop through the items array contained within the MediaLibrary class.
- Cast each item to its subtype and access its
DisplayTextproperty, passing the property's value to a
- Use a
In the Program.cs file, replace the calls to the
Display()method with a single call 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.
[MUSIC] 0:00 Hi, there. 0:00 This is James. 0:02 In this C# practice session, 0:03 you'll practice using for and foreach loops in C#. 0:07 It reinforces what you've learned in stage six of this C# objects course. 0:13 If you find this practice session too challenging to complete, 0:21 you might need to review that course. 0:26 See the teacher's notes for a link. 0:28 This practice session is the sixth in a series of sessions 0:30 where you'll build out a media library console application. 0:34 Step by step, you'll add features to the program. 0:39 Eventually, you'll be able to use C# to add, list, and search for 0:41 items like albums, books, and movies. 0:46 Whatever you want to have cataloged in your media library. 0:48 In the previous practice sessions, we created a class hierarchy for 0:52 our program's media types. 0:56 And we used encapsulation, 0:58 arrays, and properties to improve the design of our program. 1:00 In this practice session, we'll see how we can loop through our array 1:04 of media type items using two different types of loops. 1:09 The for and for each loops. 1:12 In an earlier practice session, we covered a third type of loop, while loops. 1:14 See the teacher's notes for a link to that practice session. 1:19 Go ahead and open your workspace from the previous practice session in this series. 1:23 Or you can open the workspace that I've attached to this video. 1:29 If you want, you can download the project files in order to use an external editor, 1:33 or IDE like Visual Studio. 1:38 If you open the program.cs file, you can see here in the main method, 1:41 where we're instantiating an instance of our media library class. 1:48 In passing in an array containing instances of our media type subclasses. 1:56 Our rate of media type items contains three albums, a book, and a movie. 2:05 After creating and initializing the media library instance, 2:17 we call the DetectMediaType helper method on each item in our 2:22 library in order to detect and output its subtype to the console. 2:27 Then, we called the display helper method on each item, 2:32 in order to display its information to the console. 2:37 The display method uses the C# isoperator to determine each media 2:42 type's subtype in order to explicitly cast the item to that subtype. 2:48 Casting to the subtype gives us access to the sub-classes display text property. 2:54 Listing our media library items to the console is currently a very 3:02 manual process. 3:07 We have to manually add a line of code for each new item that we add to our library. 3:09 Let's improve our program by adding a method to the media library class, 3:15 that when called, will loop through the available items and 3:21 output each item's information to the console. 3:25 For your first challenge, 3:30 add a method named DisplayItems to the media library class. 3:32 Use a for loop to loop through the items array contained within the media 3:37 library class. 3:41 As you loop through the media library items, 3:42 you'll cast each item to its subtype and access its display text property. 3:45 Passing the property's value to a console right line method call. 3:50 Then, in the Program.cs file, 3:55 replace the calls to the'Display () method with 3:58 a single call to the DisplayItems () method. 4:04 And that's your first challenge. 4:09 Good luck. 4:10 And we'll see you in the next video where I'll walk through my solution. 4:11
You need to sign up for Treehouse in order to download course files.Sign up