Hello and welcome.
In this lecture we are going to convert to string.
We are also going to truncate a value so I'm going to introduce another data type called double.
We're going to try and convert that into an integer and integers and whole number and in the process
of converting it it will be truncated.
Okay.
I've got visual studio open.
And this is where we left off in our previous lectures.
So I'm going to I'm going to comment out this code by doing two slashes and comment out on that as well.
So what I'm gonna do I'm going to create new variables one to create a variable and the data type will
be double.
I'll give it a value of k I'll set the value to three point five semicolon at the end.
Next hour reintroduce variable I and set this value to
I'm going to pass it an integer value or set a value to be an integer of k.
So basically what it will do it will convert this K here value into an integer an integer is a whole
number and in the process of doing that it will truncate truncate means because a integer does not cannot
read decimals.
So it will just leave the point five out and only display the three.
So that's what truncating means.
So next we go to to introduce our result label.
Remember we created the result label dot text so it takes in a text value.
So we're going to eventually make this convert this value as well.
Now set this to equal to the variable I as you can see as a red squiggly thing on the net because it's
complaining that that local variable is an integer we cannot implicitly implicitly convert the type
int to his drink.
Right.
We can using a helper method called two strings.
So we're going to use a helper method to do the conversion so we can make it recognize a an integer
and the way we do this where we've got I we just get rid of the semicolon and adopt.
That would give us some intelligence points so this with what we want.
We want to convert it to string due to string and place the semicolon.
All right.
So if I save that and execute this code it should output three.
It would truncate the point five.
All right.
So that's what this is.
So let me run that click on run
so if I click Okay issued output three you can see is output at three but left out the point five.
So so far we've been able to convert to string using the two string method is called a helper method.
Okay so we're going to expand on the application further using the controls we created.
So we are going to make it possible for a user to type in a number.
Remember we created a text ball but we're going to configured it in such a way that a user can type
in a number and we can perform calculation based on that number and then display the result.
Okay.
All right.
So we will need to perform at least two types of conversion.
So again the text box control stores user inputs into a string.
So we want to convert that string into an integer in order to perform a calculation on it.
The label controls stores result as a string.
We will want to convert that result of the integer calculation back into a string.
OK.
So I'm going to comment out this line of code here and introduce some other code that we can use with
our design controls.
OK.
So what I wanted we want to make this controls active.
So.
All right.
So I'm going to introduce some more different variables here now to save time.
I've already written the code here.
So I've got some code here from line 27 to 30.
So I've created another date a string type.
And given it the variable value of i and I've set it to equal to the input text box dot text I've created
a lot of variable called J which is an integer data type and I've set the value to equal to to int dot
pass and past the value of i.
Which is this textbook here.
What we're doing here we're converting this string to an integer via the int dot pass.
How permitted.
OK so the int dot pass helper method is a bit different from the two string l permitted.
Right we've used that earlier on this shows that the main difference is that the pass helper method
comes from inside of the int class and takes in a string as an input in between the method parentheses.
All right.
So this I here is referring to this input text value.
So with this piece of code here now the user input will be stored as an integer.
All right.
In the int j which is this one that will be stored as an integer and we can now perform calculations.
All right.
And display the result.
True the label control which is this one here.
All right.
So we simply what we're doing here we're converting the K to a string.
So let's save that and run the code.
Okay.
So this is the interface.
If I'm typing because we've done the conversion to not be possible to type in values I type in 7 and
click Okay.
It should give me eight because of the code.
There you go giving me eight typing in another value typing C6 you should give me seven.
All right.
The reason is adding an extra number is because of this code here.
Line twenty nine so I've sent integer k so any value up.
If I typed in as a K will give me the value of j plus 1.
All right.
So this J the integer here the pass method is doing the conversion.
All right.
So any thing are typing here as a J.
It will add one to it.
That's where we get in the the extra number from.
So if I add for there and click okay it will give me five.
What should happen if I type in a word for type in a word.
Rick I should get an arrow and click Okay.
Notice the vicious to do immediately give me an error is telling me what's going on there.
You can see visual studio as highlighted where the problem is why it failed to at runtime is giving
me the exception error.
All right.
So the problem is that the int dot pass method will only work when the string looks exactly like in
no runtime errors are quite difficult to be caught by the compiler.
We've seen the compiler errors before where you see read quickly bits before you actually compile your
code.
The runtime errors are much more difficult to catch and one of the ways to catch them is basically true
rigorous testing and one of the things you could put in place is to perform some checks on basic checks
to check some validation checks if a user provides set enabled input it will check the format make sure
the input is correct or not.
So this kind of validation will help you know address the runtime errors.
So I'll just stop the code from running by clicking that.
So I sit in this lecture we successfully converted integer to string.
We also perform a truncation.
So now set for this lecture.
Thank you so much for watching.
Bye for now.