1 00:00:00,000 --> 00:00:04,620 [MUSIC] 2 00:00:04,620 --> 00:00:06,810 Hi everyone, this is James. 3 00:00:06,810 --> 00:00:11,794 In this C# practice session, you'll get a chance to practice validating input and 4 00:00:11,794 --> 00:00:13,771 catching exceptions using C#. 5 00:00:13,771 --> 00:00:19,160 It reinforces what you learned in stage four of the C# basics course. 6 00:00:19,160 --> 00:00:22,480 If you find this practice session too challenging to complete, 7 00:00:22,480 --> 00:00:24,520 you might need to review that course. 8 00:00:24,520 --> 00:00:26,500 See the teacher's notes for a link. 9 00:00:26,500 --> 00:00:28,720 I've attached a workspace to this video. 10 00:00:28,720 --> 00:00:33,150 Go ahead and open the workspace or download the project files 11 00:00:33,150 --> 00:00:37,750 if you want to use an external editor or an IDE like Visual Studio. 12 00:00:37,750 --> 00:00:40,490 If you open the program.cs file, 13 00:00:40,490 --> 00:00:42,690 you'll see that we've provided you with some code. 14 00:00:44,330 --> 00:00:46,190 This code prompts the user for 15 00:00:46,190 --> 00:00:51,320 a number, calculates the square of the provided number, and displays the result. 16 00:00:51,320 --> 00:00:53,660 While this is a functional program, 17 00:00:53,660 --> 00:00:56,330 there are some improvements that we'd like to make. 18 00:00:56,330 --> 00:01:00,140 Each improvement is represented by a TODO code comment. 19 00:01:00,140 --> 00:01:04,480 To complete this practice session, you'll read through the TODO code comments and 20 00:01:04,480 --> 00:01:06,300 complete each one. 21 00:01:06,300 --> 00:01:08,663 Let's see the completed program in action. 22 00:01:10,913 --> 00:01:15,319 Remember, to compile a C# file, we use the mcs command. 23 00:01:19,361 --> 00:01:22,604 And to run a C# program, we use the mono command. 24 00:01:26,476 --> 00:01:30,850 See the teacher's notes if you need a refresher on either of those commands. 25 00:01:30,850 --> 00:01:34,270 Okay, the program prompts me for a number, I'll enter 2, 26 00:01:34,270 --> 00:01:37,780 then displays the square of my provided number. 27 00:01:37,780 --> 00:01:41,130 2 multiplied by itself is equal to 4. 28 00:01:41,130 --> 00:01:43,480 We can then enter another number or 29 00:01:43,480 --> 00:01:46,730 type the text quit to terminate execution of the program. 30 00:01:47,830 --> 00:01:51,700 Let's clear the console by typing clear, Enter. 31 00:01:51,700 --> 00:01:52,940 Then run the program again. 32 00:01:54,680 --> 00:01:58,161 Notice that if we don't enter a number, the program will crash. 33 00:02:02,449 --> 00:02:09,400 Or if we enter a fractional number like 4.5, the program again will crash. 34 00:02:13,165 --> 00:02:18,140 And to quit the program, we need to type the text quit in all lowercase characters. 35 00:02:18,140 --> 00:02:20,170 If we do any variation of that, 36 00:02:20,170 --> 00:02:23,970 like in all uppercase characters, the program will crash. 37 00:02:25,080 --> 00:02:27,200 As you can see, we've got some work to do. 38 00:02:29,020 --> 00:02:32,450 Let's review each of the TODO comments in the program.cs file. 39 00:02:33,510 --> 00:02:36,420 Our first task is to switch to using continue and 40 00:02:36,420 --> 00:02:40,060 break to control the flow of execution through our program. 41 00:02:40,060 --> 00:02:43,645 That will allow us to get rid of the keepGoing variable that you see defined 42 00:02:43,645 --> 00:02:44,420 right here. 43 00:02:44,420 --> 00:02:49,070 For our next task, we just need to update this prompt from Enter a number 44 00:02:49,070 --> 00:02:52,280 to Enter a number between 1 and 1,000. 45 00:02:52,280 --> 00:02:55,950 For our next task, we wanna force the user's provided value 46 00:02:55,950 --> 00:02:59,012 in the entry variable to lowercase letters. 47 00:02:59,012 --> 00:03:02,800 That will allow the user to user to type the text quit however they want. 48 00:03:02,800 --> 00:03:03,870 For this task, 49 00:03:03,870 --> 00:03:08,610 we need to add a try/catch statement to catch FormatException exceptions. 50 00:03:08,610 --> 00:03:11,410 This will keep the app from crashing if the user 51 00:03:11,410 --> 00:03:13,670 enters something that can't be parsed to a number. 52 00:03:14,900 --> 00:03:19,230 For this task, we want to allow the user to enter a fractional value. 53 00:03:19,230 --> 00:03:22,871 So instead of just whole numbers, like 1, 2, or 3, 54 00:03:22,871 --> 00:03:26,439 we want them to be able to enter 2.4, for instance. 55 00:03:26,439 --> 00:03:30,480 For the next couple of tasks, we wanna validate the user's input. 56 00:03:30,480 --> 00:03:34,050 For the first one, if the user enters a value less than or equal to 0, 57 00:03:34,050 --> 00:03:39,070 we wanna display the message, Please enter a number greater than 0. 58 00:03:39,070 --> 00:03:43,872 And for the second validation, if the user enters a value greater than 1,000, 59 00:03:43,872 --> 00:03:49,240 we wanna display the message, Please enter a number less than or equal to 1,000. 60 00:03:49,240 --> 00:03:50,790 And that's it. 61 00:03:50,790 --> 00:03:54,100 If you get stuck at any point, see the teacher's notes for hints and 62 00:03:54,100 --> 00:03:58,020 links to videos that cover what you need to know in order to complete the session. 63 00:03:58,020 --> 00:04:02,560 And if you aren't able to fully complete the program, don't worry, 64 00:04:02,560 --> 00:04:06,380 I'll show you how I completed the TODOs in the next video. 65 00:04:06,380 --> 00:04:08,860 Good luck and we'll see you in just a bit.