1 00:00:00,267 --> 00:00:03,369 Now that we know how to make methods return values, 2 00:00:03,369 --> 00:00:06,182 let's go back to our cat food store program and 3 00:00:06,182 --> 00:00:10,023 update the ask method to return the string that the user types. 4 00:00:10,023 --> 00:00:14,518 So I'm gonna go to our definition up here and I'm going to change its return type 5 00:00:14,518 --> 00:00:17,954 from void, that is returning nothing, to a type of string. 6 00:00:17,954 --> 00:00:22,568 It will return a string value. 7 00:00:22,568 --> 00:00:27,130 And instead of assigning the returned value of Console.ReadLine to the answer 8 00:00:27,130 --> 00:00:31,907 variable and printing it out, I'm just going to return that to the main function. 9 00:00:31,907 --> 00:00:36,035 So I'll get rid of the answer variable and I'll get rid of the called 10 00:00:36,035 --> 00:00:41,472 Console.WriteLine, and I'll just take the value returned from Console.ReadLine, 11 00:00:41,472 --> 00:00:44,463 and I will return that to whatever called asked. 12 00:00:44,463 --> 00:00:47,018 In this case, it's the main method. 13 00:00:47,018 --> 00:00:48,402 So let me save that. 14 00:00:50,332 --> 00:00:54,608 And then we can get rid of our attempt to access the answer variable, 15 00:00:54,608 --> 00:00:57,845 which doesn't exist down here in the main method. 16 00:00:57,845 --> 00:01:02,021 Instead we're going to take our new return value from the call to ask and 17 00:01:02,021 --> 00:01:05,022 we're gonna assign it to a variable called entry. 18 00:01:07,254 --> 00:01:11,568 And again the return value of Ask is string, so 19 00:01:11,568 --> 00:01:16,661 the type of our entry variable needs also to be string. 20 00:01:16,661 --> 00:01:20,792 And then we'll just print out the value that our entry variable contains for now. 21 00:01:20,792 --> 00:01:26,866 Let me try running this, dotnet run. 22 00:01:26,866 --> 00:01:27,763 And as before, 23 00:01:27,763 --> 00:01:31,566 it'll print out the question that gets passed in the call to ask. 24 00:01:31,566 --> 00:01:33,234 How many cans are you ordering? 25 00:01:33,234 --> 00:01:37,656 And then the call to Console.ReadLine, we'll wait for our answer, so 26 00:01:37,656 --> 00:01:39,335 we'll say ten cans again. 27 00:01:39,335 --> 00:01:43,981 Hit Enter, the value we typed gets returned from the call Console.ReadLine 28 00:01:43,981 --> 00:01:46,982 which then gets returned from the ask method, and 29 00:01:46,982 --> 00:01:49,644 gets assigned to this entry variable here. 30 00:01:49,644 --> 00:01:53,170 And then we print the value of entry out down here in main, and 31 00:01:53,170 --> 00:01:55,115 there's our printed value ten. 32 00:01:55,115 --> 00:01:58,518 Right now the user types their answer on a separate line from the question. 33 00:01:58,518 --> 00:02:03,442 That's because we print the question using Console.WriteLine which prints a string 34 00:02:03,442 --> 00:02:05,272 and then skips to the next line. 35 00:02:05,272 --> 00:02:08,744 It might look a little neater if the type to answer appeared on the same line as 36 00:02:08,744 --> 00:02:09,459 the question. 37 00:02:09,459 --> 00:02:14,150 We can do this with the Console.Write method, which prints the string and 38 00:02:14,150 --> 00:02:15,944 then stays on the same line. 39 00:02:15,944 --> 00:02:19,914 So let me update this and try running it again. 40 00:02:19,914 --> 00:02:24,199 I'll just hit the up arrow to bring up our dotnet run command and hit Enter. 41 00:02:24,199 --> 00:02:25,944 And you can see that our cursor for 42 00:02:25,944 --> 00:02:29,063 typing our response is now on the same line as the question. 43 00:02:29,063 --> 00:02:33,534 So I'll say ten cans, and everything else works the same as before. 44 00:02:33,534 --> 00:02:36,594 We've completed the second feature for our program. 45 00:02:36,594 --> 00:02:40,728 We asked the user for quantity of cans they want, call the Console.ReadLine 46 00:02:40,728 --> 00:02:44,942 method to get their keyboard entry, and store the return value in a variable. 47 00:02:44,942 --> 00:02:48,346 Learning the call methods was the key for getting this far in your program. 48 00:02:48,346 --> 00:02:51,742 Practicing your new skills is important to making them stick, so 49 00:02:51,742 --> 00:02:55,710 be sure to check the teacher's notes on this video for some practice ideas. 50 00:02:55,710 --> 00:02:58,445 You're off to a great start learning C#. 51 00:02:58,445 --> 00:03:01,605 You've learned to call methods and to use their return values. 52 00:03:01,605 --> 00:03:05,058 Those skills will be useful in pretty every program you write. 53 00:03:05,058 --> 00:03:07,069 You've even learned to write your own methods. 54 00:03:07,069 --> 00:03:09,765 There's just one minor issue with the program. 55 00:03:09,765 --> 00:03:12,356 When we ask the user how many cans they want to buy, 56 00:03:12,356 --> 00:03:16,328 our question runs right up against the place their keyboard input is shown. 57 00:03:16,328 --> 00:03:18,718 That looks a little less than professional. 58 00:03:18,718 --> 00:03:22,210 To fix this, we're going to need to learn more about strings. 59 00:03:22,210 --> 00:03:27,063 Just like most other programming languages, C# uses strings to store text. 60 00:03:27,063 --> 00:03:29,052 We'll learn all about them in the next stage. 61 00:03:29,052 --> 00:03:29,940 See you there.