1 00:00:00,150 --> 00:00:05,520 As in our previous video we started building some amount of intelligence store windows form. 2 00:00:05,520 --> 00:00:12,990 We added a click event such that when we fill out the information and select the type of car click submit. 3 00:00:13,020 --> 00:00:17,450 Then we get a message box displaying the data that was entered. 4 00:00:17,490 --> 00:00:23,160 Now in this video what we want to do is add some more functionality or some more intelligence to the 5 00:00:23,160 --> 00:00:24,030 submission. 6 00:00:24,060 --> 00:00:30,570 We want to add some validation things that maybe the customer name and so we want to make sure that 7 00:00:30,570 --> 00:00:36,720 when the person clicks submit the customer name and the car type we need to make sure that values have 8 00:00:36,720 --> 00:00:41,070 been entered focus of my name and selected for the car type. 9 00:00:41,070 --> 00:00:49,800 We also want to make sure that the dates are at least maybe the date return should not be in the POS 10 00:00:49,800 --> 00:00:51,810 or further into POS than be rented. 11 00:00:51,820 --> 00:00:57,090 You know things that that so dates oriented kinds of a future date when compared to date returns so 12 00:00:57,090 --> 00:01:02,490 we're going to add some intelligence for that and we'll also look at potential exceptions that could 13 00:01:02,490 --> 00:01:06,120 occur while we are doing our operation. 14 00:01:06,150 --> 00:01:13,650 So to start this off I'm one to modify my form a bit and what I'm want to do is add a new field and 15 00:01:13,650 --> 00:01:17,670 I'm going to call this field cost someone to go to my tool box. 16 00:01:17,670 --> 00:01:25,440 I'm going to started the display so for some reason to need my tools so I know it's displaying many 17 00:01:25,440 --> 00:01:28,360 more than I need but ultimately directly technically. 18 00:01:28,410 --> 00:01:29,690 So all. 19 00:01:29,730 --> 00:01:31,890 And then it brought all of them. 20 00:01:31,890 --> 00:01:39,410 So if you if you're a tool box or to the selections instead of your tool box disappear begin to right 21 00:01:39,420 --> 00:01:43,930 click in the pin and click show all or go to choose items and so that the ones you want. 22 00:01:43,920 --> 00:01:50,000 Right now I'm interested in common controls because I'm about to bring over a new text box. 23 00:01:50,040 --> 00:01:52,350 Drag it over and get my arrow. 24 00:01:52,410 --> 00:02:00,090 That's fine and I'm going to use this text box to allow the user to enter the costs on the stake in 25 00:02:00,090 --> 00:02:01,140 text box. 26 00:02:01,140 --> 00:02:06,610 I'm copying an existing label so I don't have to do cooking on the rework. 27 00:02:06,680 --> 00:02:12,440 Changing the property changing the text from customer name to cost. 28 00:02:12,480 --> 00:02:17,580 All right so if the expectation is that when the person is entering this record they enter the customer 29 00:02:17,580 --> 00:02:21,630 name the cost the beer into the returned and the type of car. 30 00:02:21,720 --> 00:02:27,510 No I'm going to rename the text box as we know we have developed the discipline to do because I don't 31 00:02:27,510 --> 00:02:34,620 want to have to know if it's six bucks one hour or not so using my naming convention which is to put 32 00:02:34,650 --> 00:02:40,510 the prefix TB so I'm going to see TB cost. 33 00:02:40,520 --> 00:02:46,410 All right so I know I have a cost textbooks are textbooks named costs are for the cost. 34 00:02:46,530 --> 00:02:54,020 So no I'm just going to modify my code and to get the cost I'm storing my cost as a double. 35 00:02:54,030 --> 00:02:54,360 All right. 36 00:02:54,750 --> 00:03:05,020 So I say double cost is equal to and then it would be so easy to just say TB cost not text right. 37 00:03:05,160 --> 00:03:08,490 Because that's all we know we get the text from the textbooks. 38 00:03:08,550 --> 00:03:09,570 But here's our problem. 39 00:03:09,580 --> 00:03:14,930 We're seeing that we have a red line and it C cannot implicitly convert type string to double. 40 00:03:14,940 --> 00:03:21,660 That's because text is a string and I'm trying to store the cost inside of a numeric data type which 41 00:03:21,660 --> 00:03:27,030 we know is illegal because C sharp is so strongly typed so I need to do a conversion. 42 00:03:27,060 --> 00:03:35,880 So I want to say convert dots to double and then I'm passing in the values that is in the text box. 43 00:03:35,880 --> 00:03:42,690 So what this does is it gets the takes from the text box then it converted to a double and then we store 44 00:03:42,690 --> 00:03:46,470 it inside of our variable double cost. 45 00:03:46,470 --> 00:03:50,790 Now remember that we could actually just call these variables var if we wanted to. 46 00:03:50,790 --> 00:03:56,370 So if you want to change them all that's fine I won't do it but at least we know the options that we 47 00:03:56,370 --> 00:03:56,950 have. 48 00:03:57,240 --> 00:04:03,750 After adding the cost what I'm want to do is add that data to the message box that's being printed so 49 00:04:03,750 --> 00:04:08,340 I'm just going to duplicate one of the lines and change the text accordingly cost. 50 00:04:08,440 --> 00:04:16,860 So I control D on this line we'll duplicate the line and all I'm doing is changing out the data so no 51 00:04:16,860 --> 00:04:19,140 cost is printed in my message box. 52 00:04:19,240 --> 00:04:27,390 Now before I get to the message box though I want to make sure that all the the data is available or 53 00:04:27,450 --> 00:04:29,940 is provided before it gets to it. 54 00:04:29,940 --> 00:04:37,170 So this should only get printed when everything is in place everything is successfully entered into 55 00:04:37,170 --> 00:04:37,960 the form. 56 00:04:38,100 --> 00:04:43,500 So I need to check and make sure that the customer name is available and that the type of car at the 57 00:04:43,500 --> 00:04:49,530 very least those tools should have been selected so the tool I can use for this is a simple statement 58 00:04:49,560 --> 00:04:56,520 I can simply see if the string was provided or if the value was provided then or if it wasn't provided 59 00:04:56,520 --> 00:04:58,910 rather then there is an error. 60 00:04:59,280 --> 00:05:08,350 So what I can see and I love to check the negatives first in my code I can see if customer name right. 61 00:05:08,410 --> 00:05:12,940 And then if you have programming experience or from other languages then maybe other languages would 62 00:05:12,940 --> 00:05:15,590 allow you to see is equivalent to no. 63 00:05:15,760 --> 00:05:22,630 And that seems fine because what we're checking is to see if the customer and field which we stored 64 00:05:22,690 --> 00:05:28,120 in the customer name field which is stored in the variable customer name we're checking if the value 65 00:05:28,120 --> 00:05:29,190 is no. 66 00:05:29,260 --> 00:05:34,780 That seems fine but then if it is a blank string then it's technically not low. 67 00:05:34,810 --> 00:05:38,860 So that comparison would not yield the result that we really want. 68 00:05:38,890 --> 00:05:47,200 So C sharp actually uses some string manipulation tools so I can see I can call it the data type string 69 00:05:47,470 --> 00:05:53,470 and then I can see a dot and then I will see that I have two functions here I have one that says isn't 70 00:05:53,470 --> 00:06:00,460 a large empty indicates where whether the specified string is no are empty or is what null are whitespace 71 00:06:00,460 --> 00:06:06,070 which indicates if it is no empty or consists of only whitespace characters right. 72 00:06:06,130 --> 00:06:11,890 Somewhat to choose is null or whitespace so you can choose either but isn't the law why space seems 73 00:06:11,890 --> 00:06:18,430 a bit more comprehensive in terms of what we're checking some way to use isn't a large whitespace then 74 00:06:18,430 --> 00:06:25,310 pass in the string that I'm checking to see if this string is null or contains wide spaces are it's 75 00:06:25,300 --> 00:06:31,740 just empty whatever it is that is supposed to be to return true or false right. 76 00:06:31,820 --> 00:06:44,230 Someone to say if this obtains if this is true or if the car type also is no la empties I can see if 77 00:06:44,320 --> 00:06:48,980 either one of these is no longer whitespace. 78 00:06:49,120 --> 00:06:57,680 Using the lower whitespace so if either one of these is null or contains whitespace or is empty then 79 00:06:57,740 --> 00:07:08,430 I want to display a message box that says please provide missing data please enter missing data. 80 00:07:08,430 --> 00:07:08,810 All right. 81 00:07:09,380 --> 00:07:14,690 And then as again see I'm not you know based on the fact that I'm checking for both of them at the same 82 00:07:14,690 --> 00:07:16,000 time. 83 00:07:16,140 --> 00:07:17,030 I. 84 00:07:17,360 --> 00:07:19,900 It limits my ability to be specific. 85 00:07:19,900 --> 00:07:20,170 Right. 86 00:07:20,180 --> 00:07:25,130 So if I had and if statements are a condition for the customer name alone then I could specifically 87 00:07:25,130 --> 00:07:27,240 say you're missing the customer name. 88 00:07:27,320 --> 00:07:33,050 If I had one for the car type of loan that I could specifically say you're missing the car type. 89 00:07:33,140 --> 00:07:38,930 The downside over is that I don't want to show a message box for every single error that is there one 90 00:07:38,930 --> 00:07:43,200 message box that aligns all the arrows would be nice right. 91 00:07:43,280 --> 00:07:49,860 So right now I'm just saying one message box with a generic error message based on the. 92 00:07:50,390 --> 00:07:55,390 If either one of these conditions evaluates to true. 93 00:07:55,390 --> 00:07:56,420 All right. 94 00:07:56,480 --> 00:08:03,020 Another tip that I indicated I wanted to do was on the dates because I'm getting it in and I'm getting 95 00:08:03,020 --> 00:08:05,840 dates out or dates all 10 dates in art. 96 00:08:06,140 --> 00:08:14,180 So I want to make sure that the date out doesn't have a greater data value than date in because that 97 00:08:14,180 --> 00:08:19,340 wouldn't make sense it would make sense that I see the dates or instead is the 20th of February but 98 00:08:19,340 --> 00:08:21,380 the date returned was the 1st of February. 99 00:08:21,380 --> 00:08:22,730 That's not practical. 100 00:08:23,000 --> 00:08:23,230 Right. 101 00:08:23,240 --> 00:08:28,410 So that is illegal somewhat to add another statement and I'm going to say so. 102 00:08:28,420 --> 00:08:35,420 No I'm going to specifically check for this when I have a specific message box error for this and then 103 00:08:35,510 --> 00:08:42,320 later we're going to refer to the code and make it a bit easier to or a bit more efficient in displaying 104 00:08:42,320 --> 00:08:46,980 the error messages but for no let's just work on it the way you know. 105 00:08:47,000 --> 00:08:48,230 Case by case. 106 00:08:48,230 --> 00:08:55,670 So I'm going to see if then these two are data values so it's easy enough for you as a human to compare 107 00:08:55,670 --> 00:09:00,690 two dates is February 20 greater dates then February 1. 108 00:09:00,830 --> 00:09:01,780 And that would be true. 109 00:09:01,790 --> 00:09:04,400 February 20 is agreed to date and February 1. 110 00:09:04,400 --> 00:09:08,740 So if we wanted to do this comparison I could see if the date. 111 00:09:08,760 --> 00:09:13,310 Oh it is agreed to then the date in then we have an error right. 112 00:09:13,340 --> 00:09:20,690 So dates all being the dates rented should never be greater than the date in being the date returned. 113 00:09:20,690 --> 00:09:32,440 So I can see if debt owed is greater than date in and then we display a message box seeing illegal dates 114 00:09:32,440 --> 00:09:39,910 elections and I just copy and paste that one and change the text illegal date selection. 115 00:09:39,900 --> 00:09:46,430 All right but then you noticed that I have a red line in my line thirty four with this comparison. 116 00:09:46,620 --> 00:09:53,250 And that's because we're comparing two strings it seeing you cannot use the grid to then operator when 117 00:09:53,250 --> 00:09:54,170 comparing strings. 118 00:09:54,170 --> 00:09:59,820 So this greater than operator is usually used for numbers like if I wanted to give one number is greater 119 00:09:59,820 --> 00:10:05,240 than the other if one numeric variable is greater in value than the other. 120 00:10:05,340 --> 00:10:11,460 Then fine but then I kind of compare two strings which then tells me that the way I'm handling my data 121 00:10:11,460 --> 00:10:20,160 values is inhibiting my ability to properly manipulate these values someone to go back changed my code 122 00:10:20,160 --> 00:10:20,710 a bit. 123 00:10:20,790 --> 00:10:24,960 What I'm going to do is change these notes to date time. 124 00:10:24,960 --> 00:10:35,520 So the going back to our design our eight speaker control actually returns a date time value by default. 125 00:10:35,520 --> 00:10:35,910 Right. 126 00:10:36,120 --> 00:10:40,740 So I was the one trying to convert it to a string here. 127 00:10:40,920 --> 00:10:47,610 But the value itself if we hover over our value to see that it says the time and date time can also 128 00:10:47,610 --> 00:10:48,650 be compared. 129 00:10:48,810 --> 00:10:49,620 Can be compared. 130 00:10:49,620 --> 00:10:54,060 Just like all integers and doubles and other numeric values can be compared. 131 00:10:54,300 --> 00:11:00,140 So what I'm going to do is change my code a bit and I'm not going to convert it to a string since that 132 00:11:00,210 --> 00:11:07,260 I'm getting the value and then I'm getting it error because we're putting added time inside of a string 133 00:11:07,260 --> 00:11:13,170 so I need to change my data type so no I'm just going to shortcut it and use var. 134 00:11:13,200 --> 00:11:13,830 All right. 135 00:11:13,890 --> 00:11:21,510 So var is going to make my variable take on the identity of the value of being assigned to it. 136 00:11:21,510 --> 00:11:26,160 So I'm assigning idea times I hover over the variable and all of a sudden it is at the time. 137 00:11:26,220 --> 00:11:26,530 All right. 138 00:11:26,700 --> 00:11:28,050 I noticed that my error. 139 00:11:28,080 --> 00:11:29,400 It went right away. 140 00:11:29,420 --> 00:11:35,070 So I'm checking if this daytime value is greater than this daytime value. 141 00:11:35,280 --> 00:11:42,280 So it's oats should not be greater than dates in and if it is then I'm going to display the illegal 142 00:11:42,690 --> 00:11:43,620 message. 143 00:11:43,620 --> 00:11:44,300 All right. 144 00:11:44,400 --> 00:11:50,130 The next thing that's I'm want to do in terms of the logical flaw is try and put something in place 145 00:11:50,130 --> 00:11:53,640 that it won't display this success message. 146 00:11:53,640 --> 00:11:57,430 If these conditions are true. 147 00:11:57,540 --> 00:11:58,350 All right. 148 00:11:58,380 --> 00:12:03,420 You know instinctively you're probably going to say oh but we could have said if then else if and then 149 00:12:03,510 --> 00:12:04,160 else. 150 00:12:04,290 --> 00:12:06,110 And you wouldn't be wrong. 151 00:12:06,120 --> 00:12:11,280 But then I wouldn't write it that way and I'm not one to write it that way because when we have an if 152 00:12:11,610 --> 00:12:19,140 then else if then else only one blog can evaluate the truth so that means if we say if then as if then 153 00:12:19,200 --> 00:12:23,880 else then we would only get to see one error message that is wrong with the form. 154 00:12:23,880 --> 00:12:26,310 So it's philosophical right. 155 00:12:26,340 --> 00:12:32,700 I mean there's no one way to do this because you could do it that way and you could say if this is true 156 00:12:32,700 --> 00:12:35,660 then nothing else executes if this is false. 157 00:12:35,760 --> 00:12:38,330 Else if this is true then nothing else executes. 158 00:12:38,400 --> 00:12:44,400 And then if neither one of these evaluates to true then that as would trigger and then this would be 159 00:12:44,670 --> 00:12:46,200 true and this would be displayed. 160 00:12:46,500 --> 00:12:52,730 However I want to make it that I want to show the user everything that is wrong with the farm when something 161 00:12:52,730 --> 00:12:53,490 is wrong with the farm. 162 00:12:53,490 --> 00:12:55,520 So that's why I made this statement. 163 00:12:55,530 --> 00:13:02,250 And in this Mrs. Boxer the show and this is statements this one will show also if both of these are 164 00:13:02,250 --> 00:13:05,460 wrong but then I don't want this to show at all. 165 00:13:05,730 --> 00:13:13,950 So I'm just going to put in a new variable and I'm going to call it var is valid and I'm going to set 166 00:13:13,950 --> 00:13:15,000 this to true. 167 00:13:15,000 --> 00:13:15,360 All right. 168 00:13:15,720 --> 00:13:19,220 So I'm declaring a boolean called is valid right. 169 00:13:19,230 --> 00:13:22,620 I'm calling it is valid there's no particular reason for me naming it that way. 170 00:13:22,890 --> 00:13:29,190 It's just indicative of the fact that it is tracking if the farm is in a valid states are not right. 171 00:13:29,220 --> 00:13:38,500 So VAR is valid and because I signed a valid true it is a boolean no if this condition is true and we're 172 00:13:38,550 --> 00:13:43,560 not we're going to discipline error then we know that the farm is not valid. 173 00:13:43,560 --> 00:13:44,150 All right. 174 00:13:44,220 --> 00:13:52,040 So I'm going to change the value of is valid if this statement is true then is valid is no longer true. 175 00:13:52,040 --> 00:13:52,890 All right. 176 00:13:52,890 --> 00:14:01,280 So is valid is no equal to False so once again we start off by setting it as true we want it to be true 177 00:14:01,290 --> 00:14:08,780 we hope that this form is valid but then we are going to check no for the invalid Parramatta's and the 178 00:14:08,780 --> 00:14:14,270 first of which is if the customer name or the car type was not provided then we're going to say well 179 00:14:14,390 --> 00:14:15,980 it is not valid. 180 00:14:15,980 --> 00:14:22,490 If this was not met then is valid is still true because then if this condition wasn't met then it didn't 181 00:14:22,490 --> 00:14:26,240 go in and she is a valid state or value. 182 00:14:26,240 --> 00:14:30,710 But then if the death toll is greater than the date in then the form is not valid. 183 00:14:30,740 --> 00:14:34,360 So is valid is false here. 184 00:14:34,400 --> 00:14:34,850 All right. 185 00:14:35,210 --> 00:14:39,980 So both of these will check and see that there is valid state is false. 186 00:14:40,130 --> 00:14:49,310 And then what I'm going to do in this last night is see if it is valid is equivalent to true then we 187 00:14:49,310 --> 00:14:52,340 want to proceed all right. 188 00:14:52,600 --> 00:15:00,700 So then if if is valid is true then we print our success message so it does put the success message 189 00:15:00,700 --> 00:15:07,480 instead of block knowing C sharp you can actually write this as if is valid. 190 00:15:07,480 --> 00:15:10,170 So you have two ways to write that you can see. 191 00:15:10,220 --> 00:15:11,850 Let me just read a comment. 192 00:15:11,890 --> 00:15:18,830 You can say if is valid is equivalent to truth they can write so the entire statement if it is true 193 00:15:19,250 --> 00:15:23,590 but then when you're dealing with a boolean you're really seeing if this is true. 194 00:15:24,010 --> 00:15:28,780 And then if you wanted to give it as far as against it if it is not true by just putting the exclamation 195 00:15:28,780 --> 00:15:32,570 sign instead of seeing if is valid it is equivalent to false. 196 00:15:32,580 --> 00:15:33,030 All right. 197 00:15:33,220 --> 00:15:35,470 So you can write it that Chakotay way. 198 00:15:35,470 --> 00:15:39,460 That's a nice school way to write it most programming languages elevator at it this way. 199 00:15:39,850 --> 00:15:45,850 And I'm just leaving this comment there so you can see exactly the alternatives you have. 200 00:15:45,880 --> 00:15:49,440 So if you feel more comfortable writing this way that's entirely fine. 201 00:15:49,540 --> 00:15:51,520 So let's go through this one more time. 202 00:15:51,550 --> 00:15:54,940 So we set a flag to check the validity of our forum. 203 00:15:54,940 --> 00:15:57,400 I'm just saying is valid is true. 204 00:15:57,430 --> 00:16:03,850 So when we collect all the information from the farm whereas assuming that everything is intact and 205 00:16:03,850 --> 00:16:12,760 the validity is true then we start seeing if the customer name is not provided or the car type is not 206 00:16:12,760 --> 00:16:18,990 provided then the is valid is null falls and we display the error message. 207 00:16:19,040 --> 00:16:25,750 We also check if the debt owed is greater than the date in if that holds true then it is invalid. 208 00:16:25,760 --> 00:16:29,580 And so we change a flag and display an error message. 209 00:16:29,650 --> 00:16:36,470 Now at the end of all of that we're checking to see if there is valid value is still true because if 210 00:16:36,470 --> 00:16:45,350 neither of these conditions was met then is valid is still true and if it is valid is still true then 211 00:16:45,350 --> 00:16:52,310 we display our success as long as is valid as value didn't change from true until we got here then it 212 00:16:52,310 --> 00:16:54,130 is success. 213 00:16:54,140 --> 00:17:00,320 All right so let's distort our new logic so we just click start and then I'm going to fill out the form 214 00:17:00,320 --> 00:17:07,870 for someone to keep it you know based on what we know so put in the custom of putting the cost puts 215 00:17:07,940 --> 00:17:13,100 in the type of car click submit and then it's good to say that illegal data selection. 216 00:17:13,130 --> 00:17:20,330 That's because I'm checking that the dates are printed should be greater than the date return right 217 00:17:20,330 --> 00:17:20,730 here. 218 00:17:20,740 --> 00:17:24,480 I didn't change the values on there both defaulting to the same day. 219 00:17:24,710 --> 00:17:31,340 So if I mean that's adjustment so you see that one of our validations is working I click submit and 220 00:17:31,340 --> 00:17:34,450 then we see that everything is okay. 221 00:17:34,490 --> 00:17:34,890 All right. 222 00:17:35,270 --> 00:17:36,530 No let's start breaking it. 223 00:17:36,530 --> 00:17:42,560 If I change the date rented to the fifth and the day returned is the third or the date. 224 00:17:42,580 --> 00:17:49,330 Oh it is no greater than the date in then when I click Submit illegal dates selection. 225 00:17:49,370 --> 00:17:49,860 All right. 226 00:17:50,090 --> 00:17:57,650 So we're seeing that it is hitting this if statement if we want to be more more strategic in seeing 227 00:17:57,650 --> 00:18:01,560 exactly what the court is doing we can add break points that we're already in debug mode. 228 00:18:01,610 --> 00:18:06,880 We can go back to visual studio and a breakpoint somewhere to add this breakpoint to the fore. 229 00:18:06,960 --> 00:18:09,900 Let's put it to the last variable declaration. 230 00:18:10,010 --> 00:18:10,790 All right. 231 00:18:10,790 --> 00:18:12,200 And then let's try this again. 232 00:18:12,200 --> 00:18:18,990 So I want to take all customer any in this time and then I'm going to click submit. 233 00:18:19,010 --> 00:18:24,500 And then what happens is that it hits that and then using if you live you can go line by line. 234 00:18:24,500 --> 00:18:29,990 So when you see that line highlighted in yellow it means that your breakpoint or your code execution 235 00:18:29,990 --> 00:18:31,490 is not that line. 236 00:18:31,490 --> 00:18:35,840 If you hit F 11:00 it will go to the next line and then line by line. 237 00:18:35,840 --> 00:18:37,100 You can track what's happening. 238 00:18:37,130 --> 00:18:44,410 So if I look in this variable I see that it has no data or its customer name blank string. 239 00:18:44,450 --> 00:18:47,740 If I looking car type I see that Honda is provided. 240 00:18:47,750 --> 00:18:54,560 However when I step through and I go to if you live in again it no goes into this statement because 241 00:18:54,560 --> 00:18:58,460 then this condition was just evaluated to be true. 242 00:18:58,460 --> 00:19:05,090 And so where it's seeing that the farm is not valid and then if 11 and then it goes to the message box 243 00:19:05,090 --> 00:19:12,590 and we see a message box enter missing data we click all key but then the execution is done and it continues 244 00:19:12,590 --> 00:19:18,950 and we say if you live in and then we see that the date out which is the 5th of February is greater 245 00:19:18,950 --> 00:19:22,170 than the date in which is a third of February. 246 00:19:22,250 --> 00:19:29,510 And if we if 11 it goes in again I need to make sure that it knows that the date the validity is false 247 00:19:30,440 --> 00:19:32,930 and then it displays that message box again. 248 00:19:32,930 --> 00:19:36,770 So we have two message boxes being displayed and then it continues. 249 00:19:36,770 --> 00:19:43,010 So if you live in and then we'll go to is valid no if is valid is checking if it's valid is true. 250 00:19:43,040 --> 00:19:44,860 Right right. 251 00:19:44,860 --> 00:19:50,640 No it is falls because if it wasn't said the first time it was definitely set the second time. 252 00:19:50,860 --> 00:19:56,440 And so because it's false it's actually skips over that statement and then continues. 253 00:19:56,440 --> 00:20:04,690 No at this stage we can just hit F5 for it to continue to execution or we can just click continue. 254 00:20:04,690 --> 00:20:09,250 And so we would have gotten the two message boxes because we had two arrows prisons. 255 00:20:09,250 --> 00:20:10,000 All right. 256 00:20:10,120 --> 00:20:14,980 So we see that's our validation code is work and doesn't seem we if I didn't provide a value for type 257 00:20:14,980 --> 00:20:19,420 of car we would have gotten that error message both entering missing data. 258 00:20:19,420 --> 00:20:27,850 No I'm going to run one more experiment and then show you another way that we need to code for validation. 259 00:20:27,850 --> 00:20:30,040 So I'm not willing to provide up cost. 260 00:20:30,040 --> 00:20:30,360 All right. 261 00:20:30,390 --> 00:20:32,950 So no cost will be provided. 262 00:20:32,950 --> 00:20:39,830 And then I'm going to hit submit and then you're going to see my code break right before your eyes. 263 00:20:39,830 --> 00:20:45,970 So we're getting some form of exception where see in the input string was not in a card format. 264 00:20:45,980 --> 00:20:46,270 All right. 265 00:20:46,280 --> 00:20:53,150 So what is happening here is that we did not provide a string for the cost and then it is trying to 266 00:20:53,150 --> 00:20:54,290 convert it to double. 267 00:20:54,290 --> 00:20:59,300 So essentially this is saying that I cannot convert nothing to a number. 268 00:20:59,300 --> 00:20:59,760 Yes. 269 00:20:59,780 --> 00:21:02,820 As a human you probably seeing but nothing as a number is zero. 270 00:21:02,840 --> 00:21:08,120 But a computer is seeing zero as something and nothing as literally nothing. 271 00:21:08,130 --> 00:21:08,780 Right. 272 00:21:08,810 --> 00:21:13,070 So it is seeing here that I cannot do this conversion. 273 00:21:13,070 --> 00:21:14,900 I don't know what to do here. 274 00:21:14,990 --> 00:21:17,660 And then these are what we call exceptions. 275 00:21:17,690 --> 00:21:22,970 So when we have an exception it can break our program and if I could continue to see that it exits the 276 00:21:22,970 --> 00:21:24,460 program automatically. 277 00:21:24,470 --> 00:21:24,800 Right. 278 00:21:25,220 --> 00:21:30,830 So when you're using your applications and you get those feet to Arizona equal again then the entire 279 00:21:30,830 --> 00:21:32,280 program closes. 280 00:21:32,300 --> 00:21:33,750 Those are exceptions. 281 00:21:33,860 --> 00:21:40,370 We want to reduce the number of exceptions or the likelihood that exceptions will occur and end the 282 00:21:40,420 --> 00:21:43,070 runtime or the running of our program. 283 00:21:43,070 --> 00:21:50,690 So we use exception handling because we can code defensively to some extent and we might not be able 284 00:21:50,690 --> 00:21:54,470 to code defensively to much further extent. 285 00:21:54,500 --> 00:22:02,240 So the try catch is literally the catch all that helps us to navigate around those occurrences. 286 00:22:02,240 --> 00:22:07,510 So what I'm going to introduce to this button click event is tracking. 287 00:22:07,540 --> 00:22:11,320 So going back to my first line I'm going to say try. 288 00:22:11,330 --> 00:22:16,970 So I just write Trey and then I'm going to press stop twice and it generates the entire block for me. 289 00:22:17,000 --> 00:22:22,170 So I'm trying an operation and then I'm catching an exception. 290 00:22:22,230 --> 00:22:22,900 Right. 291 00:22:22,970 --> 00:22:31,340 So let me just start by taking everything that we were doing inside of this application instead of this 292 00:22:31,340 --> 00:22:34,760 click event so adjust the code that we wrote. 293 00:22:34,760 --> 00:22:39,770 So be careful with the braces that spoke to this last brace off this statement. 294 00:22:39,770 --> 00:22:40,730 You cut that. 295 00:22:40,910 --> 00:22:47,900 And then you put it inside of the tray all right so we're trying all of these operations and not it. 296 00:22:47,910 --> 00:22:54,320 It stopped executing on the twenty seventh line or on the line where we're converting the double that 297 00:22:54,330 --> 00:22:55,550 takes to double. 298 00:22:55,590 --> 00:22:57,830 So that is the problem line. 299 00:22:57,870 --> 00:23:01,740 It doesn't have to be a problem but that's the one we've detected so far. 300 00:23:01,740 --> 00:23:08,270 The point is that I'm trying all of this wherever it feels in attempting any of this code. 301 00:23:08,280 --> 00:23:10,340 Then we go over to the catch. 302 00:23:10,380 --> 00:23:16,950 So when one of those arrows like we just saw with the conversion when that kind of error occurs then 303 00:23:16,950 --> 00:23:19,770 we catch it we catch that error. 304 00:23:19,770 --> 00:23:27,150 No I'm going to extend this catch and I'm going to put the exception into an object called E X which 305 00:23:27,180 --> 00:23:31,320 allows me to get the details of the exception being thrown. 306 00:23:31,320 --> 00:23:34,890 So that error message is a visual studio showed up both invalid. 307 00:23:34,890 --> 00:23:42,070 System Error and this and that on that I can actually get that wording from this object that I'm attaching 308 00:23:42,080 --> 00:23:43,040 to the exception. 309 00:23:43,060 --> 00:23:43,350 Right. 310 00:23:43,890 --> 00:23:55,380 So then I can display a message box specifically inside of the exception and then I can see that the 311 00:23:55,380 --> 00:23:59,610 exception or the message that I want to show is e x. 312 00:23:59,640 --> 00:24:02,850 So that's the object for the exception dot. 313 00:24:02,850 --> 00:24:05,400 And then it has message. 314 00:24:05,400 --> 00:24:06,330 All right. 315 00:24:06,330 --> 00:24:12,150 So then I can display the message saying that you know this is the problem that you're encountering 316 00:24:13,080 --> 00:24:16,380 the throttle would actually end the program. 317 00:24:16,380 --> 00:24:20,670 No you can make a decision do you want to end the program when there's an exception or do you want to 318 00:24:20,670 --> 00:24:26,440 continue with the program even though there's an exception so I can just remove the through our comments 319 00:24:26,450 --> 00:24:27,310 into told for no. 320 00:24:27,320 --> 00:24:32,880 But you can delete it if you want but the throw once again will end the program if you don't have the 321 00:24:32,880 --> 00:24:35,960 throw it will display the message box you click Okay. 322 00:24:36,180 --> 00:24:38,190 But then you still have your program running. 323 00:24:38,190 --> 00:24:39,570 So let's see how that works. 324 00:24:40,080 --> 00:24:41,790 So I go back to start. 325 00:24:41,970 --> 00:24:46,860 And then this time I want to test and make sure that my exception handling works someone to provide 326 00:24:46,860 --> 00:24:50,020 the data that I know needs to be provided. 327 00:24:50,100 --> 00:24:57,270 Change the date and then click submit and then you see input stream was not in a car for months. 328 00:24:57,270 --> 00:25:03,240 So what happened was it tried the operation it got up to the part where it tried the conversion and 329 00:25:03,240 --> 00:25:06,680 then it realized that OK I can't do this. 330 00:25:06,690 --> 00:25:08,680 It went down to catch the exception. 331 00:25:08,790 --> 00:25:15,060 And then that is the message inside of the exception because we didn't type anything in a message box 332 00:25:15,060 --> 00:25:15,750 to see this. 333 00:25:15,750 --> 00:25:16,290 Right. 334 00:25:16,290 --> 00:25:21,600 So this message box is displaying the message for that particular exception. 335 00:25:22,260 --> 00:25:28,160 So you see even though we can code defensively against fees that we're expecting or conditions that 336 00:25:28,170 --> 00:25:31,040 we're expecting business rules that we're enforcing. 337 00:25:31,110 --> 00:25:36,450 Sometimes the system itself may have an error that you could not have accounted for. 338 00:25:36,480 --> 00:25:39,880 So that is the where the try catch comes in handy. 339 00:25:39,930 --> 00:25:46,190 We can get certain exceptions and not let them in the program you know completely. 340 00:25:46,300 --> 00:25:51,540 Now as a final act I'm just going to modify the way we're displaying the message boxes. 341 00:25:51,540 --> 00:25:52,210 All right. 342 00:25:52,230 --> 00:26:00,000 So instead of this being a message box per error what's I'm going to do is have a string where I'm going 343 00:26:00,000 --> 00:26:07,140 to compile the errors and then if is valid these forms then we display that message box with all the 344 00:26:07,140 --> 00:26:09,290 compiled sentences of errors. 345 00:26:09,300 --> 00:26:12,240 So right now we have two error situations. 346 00:26:12,240 --> 00:26:14,370 We have this one and we have this one. 347 00:26:14,370 --> 00:26:15,940 We have two message boxes. 348 00:26:15,990 --> 00:26:24,270 What I'm going to do is this clear a new variable var error message but I'm making this one equal to 349 00:26:24,270 --> 00:26:25,110 an empty string. 350 00:26:25,140 --> 00:26:31,470 So I'm starting off with an empty error message because up until this point we're assuming that the 351 00:26:32,040 --> 00:26:33,820 form is valid. 352 00:26:33,820 --> 00:26:34,590 Right. 353 00:26:34,620 --> 00:26:37,130 And then the error message would be empty. 354 00:26:37,140 --> 00:26:46,470 So instead I'll see error message box that show I'm going to see instead error message. 355 00:26:46,470 --> 00:26:51,380 And then we can do a little operation here which is a plus equals. 356 00:26:51,770 --> 00:26:52,110 All right. 357 00:26:52,110 --> 00:26:55,620 So we're just saying we're compiling this string. 358 00:26:55,620 --> 00:26:56,010 All right. 359 00:26:56,190 --> 00:27:01,640 So we're seeing error message plus equals please enter missing data. 360 00:27:01,670 --> 00:27:06,230 And then I'm just going to add the new line operator right there right. 361 00:27:06,240 --> 00:27:10,170 So in case there are multiple lines that will be there at all on all the lines. 362 00:27:10,170 --> 00:27:16,500 So I can refine this a bit so I can see error please enter missing data. 363 00:27:16,530 --> 00:27:25,320 So if the customer name on the card type are not provided then I see the validity of the form is on 364 00:27:25,320 --> 00:27:32,640 all fours and then I'm adding an error message to the variable that I already created. 365 00:27:32,640 --> 00:27:32,910 Right. 366 00:27:32,910 --> 00:27:37,110 So it starts off blank and then if this is true then no it has some content. 367 00:27:37,140 --> 00:27:44,240 So I'm seeing whatever was there before I that this or this string whatever was there before. 368 00:27:44,240 --> 00:27:45,250 That's what the Plus equal. 369 00:27:45,260 --> 00:27:47,170 It's an accumulation right. 370 00:27:47,180 --> 00:27:50,020 So we would probably be used to this with math. 371 00:27:50,090 --> 00:27:53,320 Well in C sharp you can use it again strings also. 372 00:27:53,350 --> 00:27:58,880 So I'm someone to do the same thing though below I'm going to see if the date out is greater than the 373 00:27:58,880 --> 00:28:04,370 dates in then I also want to accumulate this error message onto this string. 374 00:28:04,370 --> 00:28:07,790 So I'm just going to say this new line character puts it into the string. 375 00:28:07,790 --> 00:28:14,600 Take all this extra brace and so if either one of these are if both of them are successful then we're 376 00:28:14,600 --> 00:28:19,640 just compiling the error message and that is valid would have changed anyway. 377 00:28:19,730 --> 00:28:27,410 No I can put in that if as though in here to say if is valid then display a message box with the success 378 00:28:27,760 --> 00:28:37,210 and then I'm going to see who else I want a message box so a message box that show and then I am one 379 00:28:37,210 --> 00:28:44,410 to display the arrow message whatever the contents of our message are open to this point I want to print 380 00:28:44,410 --> 00:28:46,690 them out instead of the message box. 381 00:28:46,720 --> 00:28:51,790 So that means if you add another scenario tomorrow where you have another statement for the validity 382 00:28:52,090 --> 00:28:55,990 then I can just add the error message and see what is wrong. 383 00:28:55,990 --> 00:28:59,390 So just for to put in the word there. 384 00:28:59,410 --> 00:29:00,160 All right. 385 00:29:00,160 --> 00:29:03,160 So I'm just showing you that there are so many ways you can do this. 386 00:29:03,180 --> 00:29:05,500 I'm not saying this is the way to do it. 387 00:29:05,500 --> 00:29:07,090 I'm not saying this is a standard. 388 00:29:07,090 --> 00:29:10,150 I'm not saying that this is you know there is no other way. 389 00:29:10,300 --> 00:29:12,410 I want you to have your individuality. 390 00:29:12,430 --> 00:29:12,930 Sure. 391 00:29:12,940 --> 00:29:17,890 I'm just showing you that there are so many ways to do this once you accomplish what you need to do 392 00:29:18,220 --> 00:29:24,250 and with maybe fewer lines of code the jury results on with a few lines of code means more efficient 393 00:29:24,250 --> 00:29:24,700 or not. 394 00:29:24,950 --> 00:29:27,940 But the fact is if you can accomplish it then that's good. 395 00:29:27,940 --> 00:29:30,880 So it lets us test and see what this would look like. 396 00:29:30,880 --> 00:29:38,350 So if I start the application all right and then I submits always happen when I submit is that it's 397 00:29:38,350 --> 00:29:40,000 going to bomblets on this line. 398 00:29:40,000 --> 00:29:45,550 So if I submits with everything blank it tries to convert the empty string to cost and it's just going 399 00:29:45,550 --> 00:29:46,360 to catch the error. 400 00:29:46,390 --> 00:29:48,550 So if I click submit you see them straight. 401 00:29:48,550 --> 00:29:51,220 So they catch and is displaying that error. 402 00:29:51,250 --> 00:29:52,650 So that's fine. 403 00:29:52,660 --> 00:30:00,130 Well let's provide up costs and then let's try again if I click submit no it's seeing object not set 404 00:30:00,130 --> 00:30:07,720 to instance of an object reference that's in an instance of an object which I can tell you we didn't 405 00:30:07,720 --> 00:30:09,940 write that error so that's another exception. 406 00:30:09,940 --> 00:30:15,010 So no we need to debug and see exactly what we're missing why we're getting that error. 407 00:30:15,010 --> 00:30:22,140 So if I put my breakpoint at the start of the line saw that it was minimized but my breakpoints and 408 00:30:22,140 --> 00:30:25,370 no I'm bringing back my window and I click submit. 409 00:30:25,450 --> 00:30:28,770 So I'm going to step through until I see where it jumps 0. 410 00:30:28,840 --> 00:30:35,110 So as far as you can see once it goes to the next line everything is key. 411 00:30:35,120 --> 00:30:35,510 Right. 412 00:30:35,920 --> 00:30:42,700 So it's go into the next line and then I think it's this line and seeing that the object's reference 413 00:30:42,730 --> 00:30:44,260 is not set to anything. 414 00:30:44,270 --> 00:30:48,650 So I'm here trying to convert nothing to string. 415 00:30:48,670 --> 00:30:49,040 Right. 416 00:30:49,060 --> 00:30:56,240 So the selected item is no I'm trying to convert nodes a string and then I am guaranteeing that when 417 00:30:56,240 --> 00:30:59,140 I Priscilla it's going to go straight into the catch. 418 00:30:59,140 --> 00:31:00,400 And there we go. 419 00:31:00,400 --> 00:31:02,060 All right. 420 00:31:02,200 --> 00:31:06,000 So then that's another thing that we didn't account for. 421 00:31:06,000 --> 00:31:06,310 All right. 422 00:31:06,310 --> 00:31:11,660 So that means we need to kind of change the way that we're getting the value from the combo box. 423 00:31:11,670 --> 00:31:16,710 So another way to get the value from the combo box could be the text. 424 00:31:16,720 --> 00:31:24,160 So I'll use the text so that text would get the value from the combo box and I made up code change while 425 00:31:24,160 --> 00:31:25,200 I'm in debug mode. 426 00:31:25,210 --> 00:31:28,440 So I actually have to restart the application. 427 00:31:28,440 --> 00:31:28,740 All right. 428 00:31:29,110 --> 00:31:34,690 So that text would give me the text that is being displayed in the combo box if there is no text being 429 00:31:34,690 --> 00:31:36,720 displayed then it's a blank string. 430 00:31:36,940 --> 00:31:44,050 That is C for as we're seeing if we tried the way that we had it before we're going to get an exception 431 00:31:44,410 --> 00:31:47,700 and we might not be able to properly tell the person. 432 00:31:47,710 --> 00:31:49,120 Sorry about that. 433 00:31:49,120 --> 00:31:52,240 Roman I'll be able to propagate it to the person what's wrong. 434 00:31:52,240 --> 00:31:55,820 So using the DOT text is probably easier. 435 00:31:55,840 --> 00:31:58,540 So let me just restart this application. 436 00:31:58,750 --> 00:32:04,240 Once again we restarted because if we make a code change while we're in debug mode it doesn't register 437 00:32:04,600 --> 00:32:11,030 and to restart all I did was click this our circular our B service stop button and pause buttons. 438 00:32:11,040 --> 00:32:11,280 Right. 439 00:32:11,740 --> 00:32:13,090 So let's try this again. 440 00:32:13,090 --> 00:32:17,680 So once a chance of costs when we saw the cost validation work. 441 00:32:17,700 --> 00:32:21,420 It jumped to the exception and then I'm going to click submit. 442 00:32:21,450 --> 00:32:25,130 But before I did let me take my break point because I don't want to break point. 443 00:32:25,150 --> 00:32:26,680 I don't have to step through. 444 00:32:26,710 --> 00:32:33,370 So taking all the break points I'm clicking submit and then we're seeing our message box with arrows. 445 00:32:33,370 --> 00:32:37,850 Error Please enter missing data error illegal data selection. 446 00:32:37,850 --> 00:32:38,410 All right. 447 00:32:38,470 --> 00:32:46,180 If I change one of them so put in the customer name and click submit then I'm only going to see oh well 448 00:32:46,180 --> 00:32:48,000 it's still seeing enter missing data. 449 00:32:48,000 --> 00:32:48,340 So. 450 00:32:48,640 --> 00:32:53,620 So that's another thing too because I'm checking for both customer name and type of guy in the same 451 00:32:53,740 --> 00:32:54,540 statement. 452 00:32:54,550 --> 00:32:58,250 It's hard to be individual All right. 453 00:32:58,270 --> 00:33:04,930 I mean I could probably miss it fill the toilet and say Please enter customer name and the type of car 454 00:33:05,230 --> 00:33:06,710 I could be explicit. 455 00:33:06,820 --> 00:33:10,090 So obviously 5 1 I'm not the other than the user could deduce. 456 00:33:10,090 --> 00:33:10,930 So that's fine. 457 00:33:10,930 --> 00:33:14,630 You could change a message or you could change the number of blocks. 458 00:33:14,680 --> 00:33:16,180 I won't prioritize that right. 459 00:33:16,180 --> 00:33:16,840 No. 460 00:33:16,840 --> 00:33:22,730 But let me just produce the type of car click submit and then we're seeing one error message noticing 461 00:33:22,760 --> 00:33:24,830 seeing illegal data selection. 462 00:33:24,850 --> 00:33:33,130 And then we changed that to three and then click submit and know we have a valid farm and valid data 463 00:33:33,160 --> 00:33:36,100 being inserted. 464 00:33:36,160 --> 00:33:36,530 All right. 465 00:33:36,560 --> 00:33:42,590 So that's pretty much it for building in some validations once again this is not an exact science. 466 00:33:42,590 --> 00:33:44,800 This is not an exact right. 467 00:33:44,840 --> 00:33:47,000 This is the way it has to be done. 468 00:33:47,000 --> 00:33:52,000 This is just a guide giving you some ideas as to how you could go both validating what you need to look 469 00:33:52,000 --> 00:33:58,570 out for how to properly handle when there are genuine errors that you have not accounted for without 470 00:33:58,670 --> 00:34:01,020 ending the runtime of your program.