As in our previous video we started building some amount of intelligence store windows form.
We added a click event such that when we fill out the information and select the type of car click submit.
Then we get a message box displaying the data that was entered.
Now in this video what we want to do is add some more functionality or some more intelligence to the
submission.
We want to add some validation things that maybe the customer name and so we want to make sure that
when the person clicks submit the customer name and the car type we need to make sure that values have
been entered focus of my name and selected for the car type.
We also want to make sure that the dates are at least maybe the date return should not be in the POS
or further into POS than be rented.
You know things that that so dates oriented kinds of a future date when compared to date returns so
we're going to add some intelligence for that and we'll also look at potential exceptions that could
occur while we are doing our operation.
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
I'm going to call this field cost someone to go to my tool box.
I'm going to started the display so for some reason to need my tools so I know it's displaying many
more than I need but ultimately directly technically.
So all.
And then it brought all of them.
So if you if you're a tool box or to the selections instead of your tool box disappear begin to right
click in the pin and click show all or go to choose items and so that the ones you want.
Right now I'm interested in common controls because I'm about to bring over a new text box.
Drag it over and get my arrow.
That's fine and I'm going to use this text box to allow the user to enter the costs on the stake in
text box.
I'm copying an existing label so I don't have to do cooking on the rework.
Changing the property changing the text from customer name to cost.
All right so if the expectation is that when the person is entering this record they enter the customer
name the cost the beer into the returned and the type of car.
No I'm going to rename the text box as we know we have developed the discipline to do because I don't
want to have to know if it's six bucks one hour or not so using my naming convention which is to put
the prefix TB so I'm going to see TB cost.
All right so I know I have a cost textbooks are textbooks named costs are for the cost.
So no I'm just going to modify my code and to get the cost I'm storing my cost as a double.
All right.
So I say double cost is equal to and then it would be so easy to just say TB cost not text right.
Because that's all we know we get the text from the textbooks.
But here's our problem.
We're seeing that we have a red line and it C cannot implicitly convert type string to double.
That's because text is a string and I'm trying to store the cost inside of a numeric data type which
we know is illegal because C sharp is so strongly typed so I need to do a conversion.
So I want to say convert dots to double and then I'm passing in the values that is in the text box.
So what this does is it gets the takes from the text box then it converted to a double and then we store
it inside of our variable double cost.
Now remember that we could actually just call these variables var if we wanted to.
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
have.
After adding the cost what I'm want to do is add that data to the message box that's being printed so
I'm just going to duplicate one of the lines and change the text accordingly cost.
So I control D on this line we'll duplicate the line and all I'm doing is changing out the data so no
cost is printed in my message box.
Now before I get to the message box though I want to make sure that all the the data is available or
is provided before it gets to it.
So this should only get printed when everything is in place everything is successfully entered into
the form.
So I need to check and make sure that the customer name is available and that the type of car at the
very least those tools should have been selected so the tool I can use for this is a simple statement
I can simply see if the string was provided or if the value was provided then or if it wasn't provided
rather then there is an error.
So what I can see and I love to check the negatives first in my code I can see if customer name right.
And then if you have programming experience or from other languages then maybe other languages would
allow you to see is equivalent to no.
And that seems fine because what we're checking is to see if the customer and field which we stored
in the customer name field which is stored in the variable customer name we're checking if the value
is no.
That seems fine but then if it is a blank string then it's technically not low.
So that comparison would not yield the result that we really want.
So C sharp actually uses some string manipulation tools so I can see I can call it the data type string
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
a large empty indicates where whether the specified string is no are empty or is what null are whitespace
which indicates if it is no empty or consists of only whitespace characters right.
Somewhat to choose is null or whitespace so you can choose either but isn't the law why space seems
a bit more comprehensive in terms of what we're checking some way to use isn't a large whitespace then
pass in the string that I'm checking to see if this string is null or contains wide spaces are it's
just empty whatever it is that is supposed to be to return true or false right.
Someone to say if this obtains if this is true or if the car type also is no la empties I can see if
either one of these is no longer whitespace.
Using the lower whitespace so if either one of these is null or contains whitespace or is empty then
I want to display a message box that says please provide missing data please enter missing data.
All right.
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
time.
I.
It limits my ability to be specific.
Right.
So if I had and if statements are a condition for the customer name alone then I could specifically
say you're missing the customer name.
If I had one for the car type of loan that I could specifically say you're missing the car type.
The downside over is that I don't want to show a message box for every single error that is there one
message box that aligns all the arrows would be nice right.
So right now I'm just saying one message box with a generic error message based on the.
If either one of these conditions evaluates to true.
All right.
Another tip that I indicated I wanted to do was on the dates because I'm getting it in and I'm getting
dates out or dates all 10 dates in art.
So I want to make sure that the date out doesn't have a greater data value than date in because that
wouldn't make sense it would make sense that I see the dates or instead is the 20th of February but
the date returned was the 1st of February.
That's not practical.
Right.
So that is illegal somewhat to add another statement and I'm going to say so.
No I'm going to specifically check for this when I have a specific message box error for this and then
later we're going to refer to the code and make it a bit easier to or a bit more efficient in displaying
the error messages but for no let's just work on it the way you know.
Case by case.
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
two dates is February 20 greater dates then February 1.
And that would be true.
February 20 is agreed to date and February 1.
So if we wanted to do this comparison I could see if the date.
Oh it is agreed to then the date in then we have an error right.
So dates all being the dates rented should never be greater than the date in being the date returned.
So I can see if debt owed is greater than date in and then we display a message box seeing illegal dates
elections and I just copy and paste that one and change the text illegal date selection.
All right but then you noticed that I have a red line in my line thirty four with this comparison.
And that's because we're comparing two strings it seeing you cannot use the grid to then operator when
comparing strings.
So this greater than operator is usually used for numbers like if I wanted to give one number is greater
than the other if one numeric variable is greater in value than the other.
Then fine but then I kind of compare two strings which then tells me that the way I'm handling my data
values is inhibiting my ability to properly manipulate these values someone to go back changed my code
a bit.
What I'm going to do is change these notes to date time.
So the going back to our design our eight speaker control actually returns a date time value by default.
Right.
So I was the one trying to convert it to a string here.
But the value itself if we hover over our value to see that it says the time and date time can also
be compared.
Can be compared.
Just like all integers and doubles and other numeric values can be compared.
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
I'm getting the value and then I'm getting it error because we're putting added time inside of a string
so I need to change my data type so no I'm just going to shortcut it and use var.
All right.
So var is going to make my variable take on the identity of the value of being assigned to it.
So I'm assigning idea times I hover over the variable and all of a sudden it is at the time.
All right.
I noticed that my error.
It went right away.
So I'm checking if this daytime value is greater than this daytime value.
So it's oats should not be greater than dates in and if it is then I'm going to display the illegal
message.
All right.
The next thing that's I'm want to do in terms of the logical flaw is try and put something in place
that it won't display this success message.
If these conditions are true.
All right.
You know instinctively you're probably going to say oh but we could have said if then else if and then
else.
And you wouldn't be wrong.
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
then else if then else only one blog can evaluate the truth so that means if we say if then as if then
else then we would only get to see one error message that is wrong with the form.
So it's philosophical right.
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
then nothing else executes if this is false.
Else if this is true then nothing else executes.
And then if neither one of these evaluates to true then that as would trigger and then this would be
true and this would be displayed.
However I want to make it that I want to show the user everything that is wrong with the farm when something
is wrong with the farm.
So that's why I made this statement.
And in this Mrs. Boxer the show and this is statements this one will show also if both of these are
wrong but then I don't want this to show at all.
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
this to true.
All right.
So I'm declaring a boolean called is valid right.
I'm calling it is valid there's no particular reason for me naming it that way.
It's just indicative of the fact that it is tracking if the farm is in a valid states are not right.
So VAR is valid and because I signed a valid true it is a boolean no if this condition is true and we're
not we're going to discipline error then we know that the farm is not valid.
All right.
So I'm going to change the value of is valid if this statement is true then is valid is no longer true.
All right.
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
we hope that this form is valid but then we are going to check no for the invalid Parramatta's and the
first of which is if the customer name or the car type was not provided then we're going to say well
it is not valid.
If this was not met then is valid is still true because then if this condition wasn't met then it didn't
go in and she is a valid state or value.
But then if the death toll is greater than the date in then the form is not valid.
So is valid is false here.
All right.
So both of these will check and see that there is valid state is false.
And then what I'm going to do in this last night is see if it is valid is equivalent to true then we
want to proceed all right.
So then if if is valid is true then we print our success message so it does put the success message
instead of block knowing C sharp you can actually write this as if is valid.
So you have two ways to write that you can see.
Let me just read a comment.
You can say if is valid is equivalent to truth they can write so the entire statement if it is true
but then when you're dealing with a boolean you're really seeing if this is true.
And then if you wanted to give it as far as against it if it is not true by just putting the exclamation
sign instead of seeing if is valid it is equivalent to false.
All right.
So you can write it that Chakotay way.
That's a nice school way to write it most programming languages elevator at it this way.
And I'm just leaving this comment there so you can see exactly the alternatives you have.
So if you feel more comfortable writing this way that's entirely fine.
So let's go through this one more time.
So we set a flag to check the validity of our forum.
I'm just saying is valid is true.
So when we collect all the information from the farm whereas assuming that everything is intact and
the validity is true then we start seeing if the customer name is not provided or the car type is not
provided then the is valid is null falls and we display the error message.
We also check if the debt owed is greater than the date in if that holds true then it is invalid.
And so we change a flag and display an error message.
Now at the end of all of that we're checking to see if there is valid value is still true because if
neither of these conditions was met then is valid is still true and if it is valid is still true then
we display our success as long as is valid as value didn't change from true until we got here then it
is success.
All right so let's distort our new logic so we just click start and then I'm going to fill out the form
for someone to keep it you know based on what we know so put in the custom of putting the cost puts
in the type of car click submit and then it's good to say that illegal data selection.
That's because I'm checking that the dates are printed should be greater than the date return right
here.
I didn't change the values on there both defaulting to the same day.
So if I mean that's adjustment so you see that one of our validations is working I click submit and
then we see that everything is okay.
All right.
No let's start breaking it.
If I change the date rented to the fifth and the day returned is the third or the date.
Oh it is no greater than the date in then when I click Submit illegal dates selection.
All right.
So we're seeing that it is hitting this if statement if we want to be more more strategic in seeing
exactly what the court is doing we can add break points that we're already in debug mode.
We can go back to visual studio and a breakpoint somewhere to add this breakpoint to the fore.
Let's put it to the last variable declaration.
All right.
And then let's try this again.
So I want to take all customer any in this time and then I'm going to click submit.
And then what happens is that it hits that and then using if you live you can go line by line.
So when you see that line highlighted in yellow it means that your breakpoint or your code execution
is not that line.
If you hit F 11:00 it will go to the next line and then line by line.
You can track what's happening.
So if I look in this variable I see that it has no data or its customer name blank string.
If I looking car type I see that Honda is provided.
However when I step through and I go to if you live in again it no goes into this statement because
then this condition was just evaluated to be true.
And so where it's seeing that the farm is not valid and then if 11 and then it goes to the message box
and we see a message box enter missing data we click all key but then the execution is done and it continues
and we say if you live in and then we see that the date out which is the 5th of February is greater
than the date in which is a third of February.
And if we if 11 it goes in again I need to make sure that it knows that the date the validity is false
and then it displays that message box again.
So we have two message boxes being displayed and then it continues.
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.
Right right.
No it is falls because if it wasn't said the first time it was definitely set the second time.
And so because it's false it's actually skips over that statement and then continues.
No at this stage we can just hit F5 for it to continue to execution or we can just click continue.
And so we would have gotten the two message boxes because we had two arrows prisons.
All right.
So we see that's our validation code is work and doesn't seem we if I didn't provide a value for type
of car we would have gotten that error message both entering missing data.
No I'm going to run one more experiment and then show you another way that we need to code for validation.
So I'm not willing to provide up cost.
All right.
So no cost will be provided.
And then I'm going to hit submit and then you're going to see my code break right before your eyes.
So we're getting some form of exception where see in the input string was not in a card format.
All right.
So what is happening here is that we did not provide a string for the cost and then it is trying to
convert it to double.
So essentially this is saying that I cannot convert nothing to a number.
Yes.
As a human you probably seeing but nothing as a number is zero.
But a computer is seeing zero as something and nothing as literally nothing.
Right.
So it is seeing here that I cannot do this conversion.
I don't know what to do here.
And then these are what we call exceptions.
So when we have an exception it can break our program and if I could continue to see that it exits the
program automatically.
Right.
So when you're using your applications and you get those feet to Arizona equal again then the entire
program closes.
Those are exceptions.
We want to reduce the number of exceptions or the likelihood that exceptions will occur and end the
runtime or the running of our program.
So we use exception handling because we can code defensively to some extent and we might not be able
to code defensively to much further extent.
So the try catch is literally the catch all that helps us to navigate around those occurrences.
So what I'm going to introduce to this button click event is tracking.
So going back to my first line I'm going to say try.
So I just write Trey and then I'm going to press stop twice and it generates the entire block for me.
So I'm trying an operation and then I'm catching an exception.
Right.
So let me just start by taking everything that we were doing inside of this application instead of this
click event so adjust the code that we wrote.
So be careful with the braces that spoke to this last brace off this statement.
You cut that.
And then you put it inside of the tray all right so we're trying all of these operations and not it.
It stopped executing on the twenty seventh line or on the line where we're converting the double that
takes to double.
So that is the problem line.
It doesn't have to be a problem but that's the one we've detected so far.
The point is that I'm trying all of this wherever it feels in attempting any of this code.
Then we go over to the catch.
So when one of those arrows like we just saw with the conversion when that kind of error occurs then
we catch it we catch that error.
No I'm going to extend this catch and I'm going to put the exception into an object called E X which
allows me to get the details of the exception being thrown.
So that error message is a visual studio showed up both invalid.
System Error and this and that on that I can actually get that wording from this object that I'm attaching
to the exception.
Right.
So then I can display a message box specifically inside of the exception and then I can see that the
exception or the message that I want to show is e x.
So that's the object for the exception dot.
And then it has message.
All right.
So then I can display the message saying that you know this is the problem that you're encountering
the throttle would actually end the program.
No you can make a decision do you want to end the program when there's an exception or do you want to
continue with the program even though there's an exception so I can just remove the through our comments
into told for no.
But you can delete it if you want but the throw once again will end the program if you don't have the
throw it will display the message box you click Okay.
But then you still have your program running.
So let's see how that works.
So I go back to start.
And then this time I want to test and make sure that my exception handling works someone to provide
the data that I know needs to be provided.
Change the date and then click submit and then you see input stream was not in a car for months.
So what happened was it tried the operation it got up to the part where it tried the conversion and
then it realized that OK I can't do this.
It went down to catch the exception.
And then that is the message inside of the exception because we didn't type anything in a message box
to see this.
Right.
So this message box is displaying the message for that particular exception.
So you see even though we can code defensively against fees that we're expecting or conditions that
we're expecting business rules that we're enforcing.
Sometimes the system itself may have an error that you could not have accounted for.
So that is the where the try catch comes in handy.
We can get certain exceptions and not let them in the program you know completely.
Now as a final act I'm just going to modify the way we're displaying the message boxes.
All right.
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
to compile the errors and then if is valid these forms then we display that message box with all the
compiled sentences of errors.
So right now we have two error situations.
We have this one and we have this one.
We have two message boxes.
What I'm going to do is this clear a new variable var error message but I'm making this one equal to
an empty string.
So I'm starting off with an empty error message because up until this point we're assuming that the
form is valid.
Right.
And then the error message would be empty.
So instead I'll see error message box that show I'm going to see instead error message.
And then we can do a little operation here which is a plus equals.
All right.
So we're just saying we're compiling this string.
All right.
So we're seeing error message plus equals please enter missing data.
And then I'm just going to add the new line operator right there right.
So in case there are multiple lines that will be there at all on all the lines.
So I can refine this a bit so I can see error please enter missing data.
So if the customer name on the card type are not provided then I see the validity of the form is on
all fours and then I'm adding an error message to the variable that I already created.
Right.
So it starts off blank and then if this is true then no it has some content.
So I'm seeing whatever was there before I that this or this string whatever was there before.
That's what the Plus equal.
It's an accumulation right.
So we would probably be used to this with math.
Well in C sharp you can use it again strings also.
So I'm someone to do the same thing though below I'm going to see if the date out is greater than the
dates in then I also want to accumulate this error message onto this string.
So I'm just going to say this new line character puts it into the string.
Take all this extra brace and so if either one of these are if both of them are successful then we're
just compiling the error message and that is valid would have changed anyway.
No I can put in that if as though in here to say if is valid then display a message box with the success
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
to display the arrow message whatever the contents of our message are open to this point I want to print
them out instead of the message box.
So that means if you add another scenario tomorrow where you have another statement for the validity
then I can just add the error message and see what is wrong.
So just for to put in the word there.
All right.
So I'm just showing you that there are so many ways you can do this.
I'm not saying this is the way to do it.
I'm not saying this is a standard.
I'm not saying that this is you know there is no other way.
I want you to have your individuality.
Sure.
I'm just showing you that there are so many ways to do this once you accomplish what you need to do
and with maybe fewer lines of code the jury results on with a few lines of code means more efficient
or not.
But the fact is if you can accomplish it then that's good.
So it lets us test and see what this would look like.
So if I start the application all right and then I submits always happen when I submit is that it's
going to bomblets on this line.
So if I submits with everything blank it tries to convert the empty string to cost and it's just going
to catch the error.
So if I click submit you see them straight.
So they catch and is displaying that error.
So that's fine.
Well let's provide up costs and then let's try again if I click submit no it's seeing object not set
to instance of an object reference that's in an instance of an object which I can tell you we didn't
write that error so that's another exception.
So no we need to debug and see exactly what we're missing why we're getting that error.
So if I put my breakpoint at the start of the line saw that it was minimized but my breakpoints and
no I'm bringing back my window and I click submit.
So I'm going to step through until I see where it jumps 0.
So as far as you can see once it goes to the next line everything is key.
Right.
So it's go into the next line and then I think it's this line and seeing that the object's reference
is not set to anything.
So I'm here trying to convert nothing to string.
Right.
So the selected item is no I'm trying to convert nodes a string and then I am guaranteeing that when
I Priscilla it's going to go straight into the catch.
And there we go.
All right.
So then that's another thing that we didn't account for.
All right.
So that means we need to kind of change the way that we're getting the value from the combo box.
So another way to get the value from the combo box could be the text.
So I'll use the text so that text would get the value from the combo box and I made up code change while
I'm in debug mode.
So I actually have to restart the application.
All right.
So that text would give me the text that is being displayed in the combo box if there is no text being
displayed then it's a blank string.
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
and we might not be able to properly tell the person.
Sorry about that.
Roman I'll be able to propagate it to the person what's wrong.
So using the DOT text is probably easier.
So let me just restart this application.
Once again we restarted because if we make a code change while we're in debug mode it doesn't register
and to restart all I did was click this our circular our B service stop button and pause buttons.
Right.
So let's try this again.
So once a chance of costs when we saw the cost validation work.
It jumped to the exception and then I'm going to click submit.
But before I did let me take my break point because I don't want to break point.
I don't have to step through.
So taking all the break points I'm clicking submit and then we're seeing our message box with arrows.
Error Please enter missing data error illegal data selection.
All right.
If I change one of them so put in the customer name and click submit then I'm only going to see oh well
it's still seeing enter missing data.
So.
So that's another thing too because I'm checking for both customer name and type of guy in the same
statement.
It's hard to be individual All right.
I mean I could probably miss it fill the toilet and say Please enter customer name and the type of car
I could be explicit.
So obviously 5 1 I'm not the other than the user could deduce.
So that's fine.
You could change a message or you could change the number of blocks.
I won't prioritize that right.
No.
But let me just produce the type of car click submit and then we're seeing one error message noticing
seeing illegal data selection.
And then we changed that to three and then click submit and know we have a valid farm and valid data
being inserted.
All right.
So that's pretty much it for building in some validations once again this is not an exact science.
This is not an exact right.
This is the way it has to be done.
This is just a guide giving you some ideas as to how you could go both validating what you need to look
out for how to properly handle when there are genuine errors that you have not accounted for without
ending the runtime of your program.