1 00:00:00,580 --> 00:00:01,800 How did it go? 2 00:00:01,800 --> 00:00:03,580 Let's take a look at my solution. 3 00:00:03,580 --> 00:00:08,737 In the Album.cs file, I added a constructor 4 00:00:08,737 --> 00:00:13,750 to the Album class, public Album. 5 00:00:13,750 --> 00:00:16,490 Remember, a constructor should have the same name 6 00:00:16,490 --> 00:00:18,430 as the class that it's contained within. 7 00:00:19,740 --> 00:00:24,710 Then you follow the name with a set of parentheses and a set of curly braces. 8 00:00:26,740 --> 00:00:30,290 Then I added a parameter for each of the Album's required fields. 9 00:00:32,110 --> 00:00:37,340 I only have two fields, Title and Artist, and both of them are required. 10 00:00:37,340 --> 00:00:41,600 So I added title and artist parameters, both of type string. 11 00:00:45,500 --> 00:00:47,320 Notice that I used a lowercase letter for 12 00:00:47,320 --> 00:00:50,740 my parameter names, just like I would for variable names. 13 00:00:52,260 --> 00:00:55,880 Then I set the fields to the parameter values. 14 00:00:55,880 --> 00:01:02,110 So Title with a capital t = title with a lowercase t. 15 00:01:02,110 --> 00:01:06,990 Then Artist with a capital A = Artist with lowercase a. 16 00:01:06,990 --> 00:01:11,940 Because C# is case sensitive, the compiler can understand that this Title 17 00:01:11,940 --> 00:01:15,610 with a capital T refers to the title field, 18 00:01:15,610 --> 00:01:20,550 while this title with a lowercase t refers to the title parameter. 19 00:01:20,550 --> 00:01:25,040 Then I moved on to adding constructors to the Book and Movie classes. 20 00:01:25,040 --> 00:01:29,417 First, I'll save my file, then open Book.cs. 21 00:01:33,956 --> 00:01:39,179 Public, to make sure that we can call the constructor 22 00:01:39,179 --> 00:01:44,890 outside the class, Book, parentheses, curly braces. 23 00:01:44,890 --> 00:01:49,640 Then my parameters, string title and string author. 24 00:01:50,860 --> 00:01:54,500 Then initialize the field values using the parameter values. 25 00:01:55,780 --> 00:01:59,981 Title, title, then author, 26 00:01:59,981 --> 00:02:04,997 oops, with a capital A, = author. 27 00:02:04,997 --> 00:02:08,444 Cmd+S to save the file, then open Movie.cs. 28 00:02:12,043 --> 00:02:18,303 Public, again, Movie(), curly braces. 29 00:02:18,303 --> 00:02:20,530 Add my parameters. 30 00:02:20,530 --> 00:02:24,129 So string title and string director. 31 00:02:26,356 --> 00:02:29,900 Then initialize the field values with the parameter values. 32 00:02:29,900 --> 00:02:33,550 Title with a capital T = title with a lowercase t. 33 00:02:33,550 --> 00:02:38,870 And Director, with a capital D, = director with a lowercase d. 34 00:02:38,870 --> 00:02:42,340 Command+S to save the file, or Ctrl+S on Windows. 35 00:02:42,340 --> 00:02:44,699 In the Program.cs file Main method, 36 00:02:44,699 --> 00:02:49,722 I needed to pass the necessary arguments to my immediate type class constructors 37 00:02:52,488 --> 00:02:56,850 If I don't do this, I'll get errors when I compile my program. 38 00:02:56,850 --> 00:03:00,896 Since I already had the string literal values here that I wanted to use, 39 00:03:00,896 --> 00:03:04,883 I just copied and pasted each into the appropriate constructor call. 40 00:03:16,504 --> 00:03:20,027 Then I can remove these lines of code that were setting the field values. 41 00:03:31,794 --> 00:03:33,080 And then the movie. 42 00:03:35,590 --> 00:03:39,728 First the title, Then the director. 43 00:03:43,736 --> 00:03:46,710 Going to remove some white space just to tighten this up a little bit. 44 00:03:48,400 --> 00:03:50,780 There, that looks better. 45 00:03:50,780 --> 00:03:53,080 Lastly, I compiled and ran my program. 46 00:04:00,600 --> 00:04:05,730 And here's the expected output, which is what we got last time. 47 00:04:05,730 --> 00:04:10,030 But this time, our code is using our media type class constructors 48 00:04:10,030 --> 00:04:12,580 instead of setting the field values directly. 49 00:04:12,580 --> 00:04:17,476 Adding constructors to our media type classes helps to make our code clearer and 50 00:04:17,476 --> 00:04:18,434 more concise. 51 00:04:18,434 --> 00:04:23,319 It's worth noting though that an object's field value can still be changed after 52 00:04:23,319 --> 00:04:24,815 its been instantiated. 53 00:04:24,815 --> 00:04:25,752 Let's take a look at that. 54 00:04:28,629 --> 00:04:32,659 After instantiating this Album instance and 55 00:04:32,659 --> 00:04:38,395 writing its field values to the console, let's make a change. 56 00:04:38,395 --> 00:04:41,260 Specifically, let's make a change to the Title field. 57 00:04:43,050 --> 00:04:48,395 I'll change the field value to Let It Be instead of Yellow Submarine. 58 00:04:50,631 --> 00:04:54,234 Then let's write the album field values to the console again. 59 00:05:00,203 --> 00:05:02,801 Save and compile, and run our application. 60 00:05:07,293 --> 00:05:09,911 Here we can see the album title Yellow Submarine, 61 00:05:09,911 --> 00:05:12,540 which is what we passed to the class constructor. 62 00:05:13,690 --> 00:05:15,660 And here we can see the album title, 63 00:05:15,660 --> 00:05:19,420 Let It Be, which is what we changed the field value to. 64 00:05:19,420 --> 00:05:24,430 As you saw in the C# objects course, we can update our class field definitions 65 00:05:24,430 --> 00:05:29,870 in order to prevent a field value from being changed after it's been initialized. 66 00:05:29,870 --> 00:05:32,725 To do that, we just need to add the readonly keyword. 67 00:05:37,305 --> 00:05:40,610 This may or may not be something that makes sense to do. 68 00:05:40,610 --> 00:05:44,400 Think about which object attributes might change over time and 69 00:05:44,400 --> 00:05:46,120 those that shouldn't change. 70 00:05:46,120 --> 00:05:47,569 For my Album media type, 71 00:05:47,569 --> 00:05:51,088 these field values don't have a reason to change over time. 72 00:05:51,088 --> 00:05:55,556 If they did change, it would more more sense to create a new Album instance than 73 00:05:55,556 --> 00:05:57,940 to update an existing Album. 74 00:05:57,940 --> 00:06:01,489 The same logic applies to my other media type classes, 75 00:06:01,489 --> 00:06:04,817 so I'll make the same change to those classes too. 76 00:06:09,472 --> 00:06:10,363 And Movie. 77 00:06:15,299 --> 00:06:18,030 Great job completing this practice session. 78 00:06:18,030 --> 00:06:20,730 Be sure to check out the next session in this series 79 00:06:20,730 --> 00:06:25,230 after you've completed the second stage in the C# objects course. 80 00:06:25,230 --> 00:06:27,620 Thanks for practicing with me, and we'll see you next time.