Welcome back.
In this video we're going to look at Tri pass.
And tri pass is a method that allows us to work around the problem of the user entering something that
is not a number when we're actually asking the user for a number.
So let's create a statement here.
Give me a number okay.
And now let's ask for that number.
So console dot read line and actually line.
And then we want to store whatever the user entered inside of a string that we want to then convert
into a integer.
So here input string okay.
So now I want to convert that input string into a number.
And we saw how that works right.
We created this variable int num one is going to be int dot parse.
And then we add the input string in here.
And then we can just write out what the user has entered.
So we could add something to it.
Let's say we increment the number okay.
Like so just to keep it super simple.
So user entered number plus one.
And then we just use the num one.
Whatever num one at that point is okay.
So if we now run this code we will see if we give a number five our program runs and it closes because
we don't have a console dot read key here.
Let's run this again.
So we enter something and it says user entered number plus one is six okay so that worked.
But what if we don't enter a number.
We saw what happens there.
So something like this we get a format exception.
So we get an error because the format isn't right.
How do we get our way around it.
Well there is this other thing called try parse.
So we can use int dot try parse.
And it needs to have an input string.
And it needs to know where to store the result.
So where to store the result of that conversion.
So if I do it like this, what we'll do or what it will do is let me actually set it up like that.
It will now get the user input.
It will try to parse it, and if it worked it will store the result in num one.
So let me run this and let's enter.
Hi.
And then it says user entered number plus one is one.
So why did our program not crash.
Well because of our try parse.
Why do we get one.
Because integers by default are set to zero.
So this int here is zero even though we didn't assign any value to it.
So by default it's like this okay we didn't add anything here.
But still that's what happened.
So if we run it again and now we enter a number, then you see it works.
And if we don't enter a number we still get the one in the end.
So how can we now use this in order to figure out if the user has entered a right number or not.
So we can, for example, use something like if statements.
Okay.
So we can just use this entire statement here inside of an if statement.
Because if you hover over this, try parse over this method.
You will see what it returns.
It returns a bool.
So it says give me a string and give me an out.
But then I will return a bool.
So what is a bool?
A bool is a variable that can be either true or false.
So in order to do it right, let's create a bool here and let's call it is a number okay.
So this boolean will now hold a true or false statement depending on what the user has entered.
So now let me add a break point here just to see.
So let's enter a number.
Then we get to this point where it's trying to convert it.
It says is number is false.
That's because a bool by default is set to false.
It never is true by default.
So only when you override it it will take the true value.
So the value of true.
So then I can step over.
And now I hover over and you see is number now is set to true.
That's because I did in fact enter a number.
Now let me run this again.
Now this time I'm going to add a few letters to my number.
Then suddenly it's not the number anymore.
It's a string.
So now it says it's false because that's the default value.
I go to the next line, I hover over it and his number is still false.
That's because now I actually didn't enter a number because this try parse returned false because it
couldn't convert whatever I entered into a number.
So how can I use this now?
I can use this in an if statement.
I can say if is a number I'm going to say Console.WriteLine.
Well done.
You entered a number and otherwise if it's not a number, I'm going to say ha ha, you troll.
You should have entered a number.
Okay, so let's run our software again.
Let's enter something and then it says, haha, you troll, you should have entered a number and this
is how you can use try pass.
Okay, you use it in order to convert a string into a integer number.
And if it does work, then you get a number out and if it doesn't work, then you don't get the number
out, but your bool is going to stay false.
Okay, so that's it for this video.
Now you know how to use try Pass.
And you can use that in order to make sure that the software that we have built before this addition
calculator, that it doesn't crash anymore if the user entered something that is not a number.
All right.
So that's it for this video.
See you in the next one.