Instantiating Class Instances3:58 with James Churchill
In this video, we'll review the solution to the first challenge and introduce our second challenge—instantiating instances of the classes that we created in the first challenge.
- In the Program.cs file
Main()method, use the
newkeyword to create at least one instance of each of your media type classes.
- For each of the objects that you create, set each field to an appropriate value.
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.
Welcome back, how did it go? 0:00 It's okay if you weren't unable to complete every part of the challenge. 0:02 It's not unusual to struggle when you're learning something new. 0:05 Let's walk through my solution. 0:09 I started by adding a .cs file for each of my media types. 0:10 Remember that it's a convention for the file name to match the class name. 0:15 So File > New File then Album.cs. 0:20 Then File > New File, Book.cs. 0:27 And then File > New File, again, and Movie.cs. 0:34 In the Album.cs file, I then added the Treehouse.MediaLibrary namespace. 0:41 Then, within the namespace code block, I added the class definition. 0:53 The class keyword, followed by the name of the class, Album, and 0:58 a set of curly braces. 1:02 To save a bit of typing, I copied and pasted this code into the other files, 1:07 And changed the class name to the correct name for that file. 1:15 Then I added the fields to each class. 1:24 For the Album class, I added Title and Artist fields, both of type string. 1:29 So public string Title, then public string Artist. 1:34 Each of my media types has a Title field, so 1:41 I'm going to copy this line of code to the clipboard. 1:45 Then in Book, I'll paste that line of code, And 1:49 add one more field, public string Author. 1:53 Then in the Movie class, I'll paste from the clipboard again and 1:59 add the second field, public string Director. 2:03 Once I finished writing the code for the classes, I open the console and 2:09 use the mcs command to compile my program, 2:13 just to make sure that I didn't make any mistakes in my code. 2:16 mcs, and then I'll use *.cs to include all of the .cs files in my project's folder. 2:20 And I'll include -out:Program.exe, 2:27 which is the name of the exe file that I want the compiler to create. 2:31 All right, I didn't receive any compiler errors, so 2:43 it looks like I didn't make any mistakes, at least so far. 2:46 I did receive some warnings, but that's because we're not using the media type 2:49 classes and setting any of the field values. 2:54 Let's fix that now. 2:57 Remember that a class is a template for 2:59 making individual objects of a particular type. 3:01 For example, using the Album class, I can create three objects, or 3:04 instances, of the Album class. 3:09 For the second challenge, use the new keyword in the Program.cs file Main 3:11 method to create at least one instance of each of your media type classes. 3:15 Then, for 3:21 each of the objects that you create, set each field to an appropriate value. 3:22 I created three media types, so I'll create or 3:27 instantiate an instance of the Album, Book, and Movie classes. 3:30 Then for the Album class instance, I'll set values on the Title and Artist fields. 3:36 And for the Book class instance, I'll set values on the Title and Author fields. 3:42 And for the Movie class instance, I'll set values on the Title and Director fields. 3:48 And that's it for the second challenge. 3:53 After the break, I'll show you my solution. 3:55
You need to sign up for Treehouse in order to download course files.Sign up