Working with Enums3:57 with Carling Kirk
We'll learn about enums and create one in our GameResult class.
Let's get back to building our game result class. 0:00 The next field in our csv is the team name. 0:03 That's pretty easy, it's a string. 0:06 Public string 0:08 TeamName, get, set. 0:13 Then in our read soccer results method we can just assign 0:19 it to the second element in our array. 0:22 GameResult.TeamName equals values(1). 0:28 Let's see what's next. 0:36 Next we've got our HomeOrAway field. 0:40 You can have two string values, HomeOrAway. 0:44 We could just store the value as a string, 0:49 but I think there's a better way to store those values. 0:51 We briefly talked about the enum type before when we use string.split. 0:55 Let's create an enum to represent our home or away value. 0:59 Public enum HomeOrAway. 1:08 Home and Away. 1:17 This enum has two possible constant values Home and Away. 1:20 Each one has a backing integer starting with zero by default. 1:26 We can also declare other values for each constant. 1:32 This is typically done when the backing value has some kind of significance. 1:40 But in our case, it's not necessary. 1:44 We can also change the enum's underlying type to another integral type. 1:49 For instance, we could use byte. 1:53 You won't typically see this, though. 1:58 It's more common to leave it as an integer. 2:01 Now, we can create a property to hold the home or 2:06 away value inside our game result class. 2:09 Public, HomeOrAway. 2:11 HomeOrAway. 2:15 Get set. 2:19 We can go back and parse the home or away field to our gamers old object. 2:23 We can use the triparse method of the enum class to parse our string value into 2:28 the enum we declared. 2:32 HomeOrAway, homeOrAway and then if. 2:37 This is gonna be a lot like our DateTime.TryParse except 2:43 we'll do enum.TryParse. 2:47 Then we'll pass it values 2 and 2:52 then out homeOrAway, and if the parse is 2:57 successful, gameResult.HomeOrAway gets assigned to homeOrAway. 3:04 At this point, we should change the return type of this method to a list of game 3:11 result objects, instead of a list of string arrays. 3:15 List Of GameResult and 3:20 list of GameResult. 3:27 And down here, instead of our string array, we'll add gameResult. 3:33 Let's set a break point here. 3:37 And we'll see if our parsing is working so far. 3:42 F5. 3:44 Looking good. 3:54
You need to sign up for Treehouse in order to download course files.Sign up