Working with DateTime6:35 with Carling Kirk
We'll learn about the DateTime struct and parse date values from our CSV file.
Let's start by creating our class first. 0:01 We'll right-click on the project choose Add > Class and 0:03 we'll name it GameResult. 0:09 Let's open up our CSV file and check out the data. 0:12 Our first field is GameDate. 0:17 It's got both a date and a time in the field. 0:20 We can use a date time in C# to represent this date and time. 0:23 Let's make the class public and 0:29 public DateTime GameDate, get, set. 0:34 A date time in C# is a struct, we mentioned structs before, 0:43 let's talk a little more about them before we get into using DateTime. 0:47 Earlier we said that a struct is a lot like a class, but 0:51 it has some limitations, but it also has some advantages. 0:54 A struct takes up less memory than a class and 0:58 its members are usually very closely related. 1:01 There are a few case in which you might want to create one yourself but 1:04 it's not very common in everyday development. 1:08 However, it's important to know the differences between a struct and 1:10 a class for when you happen upon them in your projects. 1:14 A struct is a value type. 1:17 The other value types in C# are ones we've been using, integers, booleans, and bytes. 1:19 Here's a table that lists all the value types in C#. 1:26 Here's our struct. 1:33 In C#, we've also got reference types. 1:35 When we create an object from a class, that object is a reference type. 1:38 The big difference between a value type and 1:44 a reference type is about how it's stored in memory. 1:46 When we create an object from a class and assign it to a variable, 1:50 the variable holds the reference to the object and not the object itself. 1:54 It's like an address. 1:59 I can write down the address of a house on multiple pieces of paper, but 2:01 the piece of paper doesn't hold the actual house. 2:05 It tells us where the house is located. 2:08 We can create multiple variables and assign them to the same object, but 2:11 there's only one copy of that object stored in memory. 2:15 And all the variables only hold a reference to that one object. 2:19 But when we create a value type, it doesn't hold a reference, 2:23 it holds the actual value. 2:27 If we create multiple variables and 2:30 assign them to a value type, each of those variables will hold a copy of the value we 2:32 assign our date time struct represents a single point in time. 2:36 It has a static property on it that you'll use quite often as a developer, 2:42 DateTime now. 2:47 Let's check it out in the C# interactive window. 2:48 View > Other Windows > C# Interactive. 2:51 DateTime.Now. 2:57 DateTim.Now returns a DateTime object that represents the point in time it was 3:03 assigned, according to the system that the application is running on. 3:07 The DateTime object itself has some helpful properties, that lets us access 3:12 components of the date and time like hour or minute or even day of the week. 3:17 DataTime.Now.DayOfWeek. 3:21 We need to parse the game date field in our CSV file into a DateTime object so 3:28 we can store it in our game result class. 3:32 Let's do that inside our read soccerResults method. 3:35 The first line of our file contains the header values from our spreadsheet, but 3:41 we don't really need that. 3:45 Before our while loop here, we can read the first line to sort of throw it away 3:47 for now, reader.ReadLine, then the position of the reader 3:52 in the file will be at our first line of data, which is what we really care about. 3:58 Let's instantiate a new GameResult. 4:03 var gameResult = new 4:05 GameResult Now we can assign a value 4:10 to our gameDate property, but we'll need to convert it to a date-time first. 4:17 The date-time struct has a method on it called parse. 4:22 We can call it on the first element of our values array, which is the gameDate value. 4:27 gameResult.GameDate = 4:31 DateTime.Parse values, first one in the array. 4:39 But what would happen if the value in the array element wasn't a valid date like 4:51 if it were null or in the wrong format? 4:55 We would get an exception. 4:58 We could add a block here to make sure it's handled, but 5:00 there's a better way to parse the string into a DateTime with the try parse method. 5:04 DateTime.TryParse values, 5:09 First one in the array and we need an out parameter gameDate. 5:17 The TryParse method returns a boolean value if the parse was successful. 5:25 And it takes two parameters the string to parse and 5:29 a DateTime value that will hold the result of the parse operation. 5:32 The out keyword here is indicating that the value was passed as a reference. 5:36 Since the DateTime type is a value type, 5:41 we're indicating that it should be passed as a reference. 5:44 When you pass a value type into a method without it, 5:47 it creates a copy of the value. 5:50 We need to declare the DateTime variable though, to hold the value. 5:53 Date time gameDate, 5:58 then we can add an if statement around the TryParse, and if the parse was successful, 6:03 well, we'll assign the game date value to the property in our game result. 6:07 If, TryParse is successful, 6:12 then the gameResult .GameDate gets the game date value. 6:18 And we can delete this line here. 6:27 And so, if the TryParse isn't successful our program will just skip it. 6:30
You need to sign up for Treehouse in order to download course files.Sign up