1 00:00:01,580 --> 00:00:03,940 Let's start by creating our class first. 2 00:00:03,940 --> 00:00:09,949 We'll right-click on the project choose Add > Class and 3 00:00:09,949 --> 00:00:12,897 we'll name it GameResult. 4 00:00:12,897 --> 00:00:15,470 Let's open up our CSV file and check out the data. 5 00:00:17,781 --> 00:00:20,580 Our first field is GameDate. 6 00:00:20,580 --> 00:00:23,560 It's got both a date and a time in the field. 7 00:00:23,560 --> 00:00:27,200 We can use a date time in C# to represent this date and time. 8 00:00:29,900 --> 00:00:34,595 Let's make the class public and 9 00:00:34,595 --> 00:00:43,080 public DateTime GameDate, get, set. 10 00:00:43,080 --> 00:00:47,050 A date time in C# is a struct, we mentioned structs before, 11 00:00:47,050 --> 00:00:51,010 let's talk a little more about them before we get into using DateTime. 12 00:00:51,010 --> 00:00:54,250 Earlier we said that a struct is a lot like a class, but 13 00:00:54,250 --> 00:00:58,810 it has some limitations, but it also has some advantages. 14 00:00:58,810 --> 00:01:01,650 A struct takes up less memory than a class and 15 00:01:01,650 --> 00:01:04,870 its members are usually very closely related. 16 00:01:04,870 --> 00:01:08,150 There are a few case in which you might want to create one yourself but 17 00:01:08,150 --> 00:01:10,950 it's not very common in everyday development. 18 00:01:10,950 --> 00:01:14,240 However, it's important to know the differences between a struct and 19 00:01:14,240 --> 00:01:17,570 a class for when you happen upon them in your projects. 20 00:01:17,570 --> 00:01:19,600 A struct is a value type. 21 00:01:19,600 --> 00:01:25,130 The other value types in C# are ones we've been using, integers, booleans, and bytes. 22 00:01:26,600 --> 00:01:29,548 Here's a table that lists all the value types in C#. 23 00:01:33,886 --> 00:01:35,918 Here's our struct. 24 00:01:35,918 --> 00:01:38,730 In C#, we've also got reference types. 25 00:01:38,730 --> 00:01:42,940 When we create an object from a class, that object is a reference type. 26 00:01:44,400 --> 00:01:46,650 The big difference between a value type and 27 00:01:46,650 --> 00:01:49,230 a reference type is about how it's stored in memory. 28 00:01:50,520 --> 00:01:54,530 When we create an object from a class and assign it to a variable, 29 00:01:54,530 --> 00:01:58,430 the variable holds the reference to the object and not the object itself. 30 00:01:59,470 --> 00:02:01,180 It's like an address. 31 00:02:01,180 --> 00:02:05,180 I can write down the address of a house on multiple pieces of paper, but 32 00:02:05,180 --> 00:02:08,640 the piece of paper doesn't hold the actual house. 33 00:02:08,640 --> 00:02:10,350 It tells us where the house is located. 34 00:02:11,470 --> 00:02:15,660 We can create multiple variables and assign them to the same object, but 35 00:02:15,660 --> 00:02:19,130 there's only one copy of that object stored in memory. 36 00:02:19,130 --> 00:02:22,460 And all the variables only hold a reference to that one object. 37 00:02:23,690 --> 00:02:27,350 But when we create a value type, it doesn't hold a reference, 38 00:02:27,350 --> 00:02:30,000 it holds the actual value. 39 00:02:30,000 --> 00:02:32,090 If we create multiple variables and 40 00:02:32,090 --> 00:02:36,710 assign them to a value type, each of those variables will hold a copy of the value we 41 00:02:36,710 --> 00:02:42,540 assign our date time struct represents a single point in time. 42 00:02:42,540 --> 00:02:47,218 It has a static property on it that you'll use quite often as a developer, 43 00:02:47,218 --> 00:02:48,207 DateTime now. 44 00:02:48,207 --> 00:02:51,766 Let's check it out in the C# interactive window. 45 00:02:51,766 --> 00:02:57,395 View > Other Windows > C# Interactive. 46 00:02:57,395 --> 00:03:03,461 DateTime.Now. 47 00:03:03,461 --> 00:03:07,700 DateTim.Now returns a DateTime object that represents the point in time it was 48 00:03:07,700 --> 00:03:10,900 assigned, according to the system that the application is running on. 49 00:03:12,090 --> 00:03:17,006 The DateTime object itself has some helpful properties, that lets us access 50 00:03:17,006 --> 00:03:21,708 components of the date and time like hour or minute or even day of the week. 51 00:03:21,708 --> 00:03:26,088 DataTime.Now.DayOfWeek. 52 00:03:28,238 --> 00:03:32,871 We need to parse the game date field in our CSV file into a DateTime object so 53 00:03:32,871 --> 00:03:35,427 we can store it in our game result class. 54 00:03:35,427 --> 00:03:37,799 Let's do that inside our read soccerResults method. 55 00:03:41,975 --> 00:03:45,634 The first line of our file contains the header values from our spreadsheet, but 56 00:03:45,634 --> 00:03:47,880 we don't really need that. 57 00:03:47,880 --> 00:03:52,370 Before our while loop here, we can read the first line to sort of throw it away 58 00:03:52,370 --> 00:03:58,330 for now, reader.ReadLine, then the position of the reader 59 00:03:58,330 --> 00:04:03,470 in the file will be at our first line of data, which is what we really care about. 60 00:04:03,470 --> 00:04:05,255 Let's instantiate a new GameResult. 61 00:04:05,255 --> 00:04:10,518 var gameResult = new 62 00:04:10,518 --> 00:04:17,330 GameResult Now we can assign a value 63 00:04:17,330 --> 00:04:22,078 to our gameDate property, but we'll need to convert it to a date-time first. 64 00:04:22,078 --> 00:04:25,510 The date-time struct has a method on it called parse. 65 00:04:27,150 --> 00:04:31,516 We can call it on the first element of our values array, which is the gameDate value. 66 00:04:31,516 --> 00:04:39,836 gameResult.GameDate = 67 00:04:39,836 --> 00:04:49,060 DateTime.Parse values, first one in the array. 68 00:04:51,040 --> 00:04:55,830 But what would happen if the value in the array element wasn't a valid date like 69 00:04:55,830 --> 00:04:58,260 if it were null or in the wrong format? 70 00:04:58,260 --> 00:05:00,460 We would get an exception. 71 00:05:00,460 --> 00:05:04,373 We could add a block here to make sure it's handled, but 72 00:05:04,373 --> 00:05:09,460 there's a better way to parse the string into a DateTime with the try parse method. 73 00:05:09,460 --> 00:05:15,191 DateTime.TryParse values, 74 00:05:17,977 --> 00:05:25,276 First one in the array and we need an out parameter gameDate. 75 00:05:25,276 --> 00:05:29,800 The TryParse method returns a boolean value if the parse was successful. 76 00:05:29,800 --> 00:05:32,710 And it takes two parameters the string to parse and 77 00:05:32,710 --> 00:05:36,950 a DateTime value that will hold the result of the parse operation. 78 00:05:36,950 --> 00:05:41,828 The out keyword here is indicating that the value was passed as a reference. 79 00:05:41,828 --> 00:05:44,005 Since the DateTime type is a value type, 80 00:05:44,005 --> 00:05:47,570 we're indicating that it should be passed as a reference. 81 00:05:47,570 --> 00:05:50,870 When you pass a value type into a method without it, 82 00:05:50,870 --> 00:05:52,330 it creates a copy of the value. 83 00:05:53,370 --> 00:05:56,230 We need to declare the DateTime variable though, to hold the value. 84 00:05:58,488 --> 00:06:03,290 Date time gameDate, 85 00:06:03,290 --> 00:06:07,770 then we can add an if statement around the TryParse, and if the parse was successful, 86 00:06:07,770 --> 00:06:12,230 well, we'll assign the game date value to the property in our game result. 87 00:06:12,230 --> 00:06:18,472 If, TryParse is successful, 88 00:06:18,472 --> 00:06:25,560 then the gameResult .GameDate gets the game date value. 89 00:06:27,120 --> 00:06:29,180 And we can delete this line here. 90 00:06:30,500 --> 00:06:34,360 And so, if the TryParse isn't successful our program will just skip it.