Adding Class Constructors5:36 with James Churchill
In this video, we'll review the solution to the second challenge and introduce our third and final challenge—adding constructors to our classes.
- Add a constructor to each of your media type classes.
- Define a constructor parameter for each of the media type's required fields and use those parameter values to initialize its fields.
- Update the Program.cs file
Main()method in order for your program to successfully compile.
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.
How did you with the second challenge? 0:00 Let's walk through my solution. 0:02 In the program.cs file main method, 0:04 I used the new keyword to create an instance of each of my media type classes. 0:07 First a variable, var album 0:13 = new Album with a capital A followed by a set of parentheses. 0:16 Then var book = new Book. 0:21 Then var movie = new Movie. 0:29 Then I set each object field to a value. 0:34 First the album. 0:38 So album.Title = and 0:39 then how about Yellow Submarine? 0:43 Then album.artist = "The Beatles". 0:49 For the book, how about 0:57 book.title = Moby Dick and 1:02 book.author equals Herman Melville. 1:08 And for the movie, 1:23 movie.Title = Lawrence of Arabia and 1:26 movie.Director = David Lean and 1:33 lastly I compiled my program. 1:39 View, Show Console, 1:44 mcs *.cs-out:Program.exe. 1:48 Now let's try running the program. 1:57 So mono Program.exe and 1:58 nothing happened. 2:03 Let's take another look at our code. 2:07 So here's our static void main method. 2:10 That's the method that's getting called when we ran our application, but notice 2:13 that I instantiated each of my media type classes, and then I set field values. 2:16 But I didn't do anything, I didn't write anything to the console. 2:22 Let's fix that. 2:25 For now, let's just write each object's field values to the console. 2:28 So console.WriteLine, then for 2:33 the value I'll start with a string literal, 2:38 album: " + plus album.title 2:45 + " by " + album.Artist); 2:50 let's repeat that again for 2:55 both the book and the movie. 3:00 To save some typing I'll copy and paste. 3:04 Change Album to Book and then Album here to book and 3:11 then instead of album.Artists it's book.Author. 3:14 And then instead of album 3:18 it's Movie Movie.title by 3:24 movie.Director. 3:34 Now let's compile and run the program again. 3:39 This time, I'll run both commands at the same time. 3:47 So, ncs*.cs -out: Program.exe 3:49 && mono Program.exe. 3:56 And this time, we got our media type information written to the console. 4:01 Great work. 4:05 Let's review how we're setting our field values. 4:07 Currently, we're creating a class instance, then writing a line of code for 4:10 each field value that we want to set. 4:14 As we've seen, this works fine but 4:17 if we had a lot of fields, this approach could become tedious. 4:19 Furthermore, when thinking about these media types, setting a value for 4:23 each of these fields doesn't really feel optional. 4:27 Though, with our current approach, 4:30 it's perfectly valid at least as far as the C# compiler's concern to create 4:33 an instance of any of these media types, and not set any of the field values. 4:38 To fix this, we can add a constructor to each of our media type classes, 4:45 each constructor can define a parameter for 4:50 each field that should have a value, the required fields for that media type. 4:53 For example, here in my album class, I'll add a constructor with title and 4:58 artist parameters and then use those parameters to initialize these fields. 5:02 For the third and last challenge in this practice session, 5:09 add a constructor to each of your media type classes. 5:11 Define a parameter for each of the required fields and 5:16 use those parameter values to initialize the fields. 5:19 After adding your class constructors, you'll need to update the code 5:24 in the Program.cs file 'Main()' method in order for 5:27 your program to successfully compile. 5:30 And that's it for the third challenge. 5:32 See you in a bit. 5:35
You need to sign up for Treehouse in order to download course files.Sign up