Welcome back.
In this video I want to go over conversion helpers.
And we have seen one of those.
That is the parse method.
I just want to quickly show you the parse method once again.
And that is for example I have this string which just holds a number and I am parsing that string.
And I obviously need to give it a name.
So number string for example.
And I now am going to parse this number string into an int by using the parse method.
Okay.
So this is how I can use parsing.
And this parse here is a conversion helper okay.
So I am now using this string which holds only a number.
It cannot hold a character in here that is not a number.
So I cannot enter a B in here or whatever.
This would cause an error.
This would crash our application.
If there is really just a integer number inside of this number string, then I can parse it into an
int and this will work.
And then result will be 1 to 3.
Now in some cases you need to use the convert method.
So there is let's say the string I'm going to call this one my bool string okay.
Because it's just going to hold a string that will say something like true for example.
So it can only say true or false.
Because what I want to do is I want to convert that now into a bool variable.
So into a boolean.
So I can just say bool.
My bool and my bool will now be whatever my bool string is going to hold.
However, in order to bring it into the right data type, I need to use the convert class.
So there's this class which is called convert and it has a bunch of methods.
So a class is just a summary of a lot of information.
And we're going to see what kind of information a class can hold.
But it can also have actions.
So it can have capabilities slash skills so methods that it can execute.
We've seen the console class and used its methods quite a bit like read key and write line.
Now we're using the convert class which has a method that is called two.
And then you can see it can convert into many different types.
It can convert into an int 32.
It can convert into an S byte into a double decimal datetime.
It can convert into many different types.
But what I want to do in this particular case, I want to convert my bool string into a bool.
Okay, now it will take this bool string and it will try to convert whatever is in here into an actual
bool data type.
Okay, so if I run this code now and I see that I don't get an error, all is good.
Okay, so let's just write something onto the console where I'm going to say my bool is.
And then I can use plus my bool here okay.
Let's see what's going to come from it.
And you see my bool is true.
So it did convert my my statement from here true into a bool.
And then we used that in order to display it.
So what's happening here is that it's also implicitly converting a bool into a string.
Because this line here we're using a string and it just says okay.
Yeah.
You know what?
I'm going to take this thing here and make a string out of it.
That's because in C sharp you can convert any object into a string, even though it might not be very
readable, but it also works for structs, which boolean is.
So a boolean is internally a struct.
We're going to see what structs are a lot later, so don't worry about it for now.
You just need to know that the bool can hold two values.
It can be true or false.
So if I just enter something wrong in here like one two, three for example, and I run it again, you
will see I don't get anything.
So my bool cannot.
So this line here, I actually get an error here.
It says it cannot convert it.
It says format exception.
So it's trying to convert something that is a string of 1 to 3 into an actual bool.
But that doesn't work.
So here you need to be super precise.
You need to be really careful that whatever you have inside of your variable that you want to convert
is actually a value that can be of the type that you are trying to convert it to.
So in this case, the value here has to be either true or false.
Both work, but it has to be one of those two.
And then you can see my bool is false.
So you see it writes it with a capital letter.
So we could also write false like this with a capital letter.
It would work as well.
That's what this two boolean method takes care of for us automatically.
So it really just converts it into the right type.
If it's written false with a capital F or small F or whatever.
Okay.
But it has to be false or true in this particular case now.
As we've seen, however, the convert class also holds other methods or has other methods.
So convert dot.
You can see it can convert something into a string, so it can convert the bool back into a string.
For example, it can convert something into a number into a char and so forth.
However, what you are passing in here inside of those brackets has to somehow be actually convertible
into that type.
So things can go wrong here.
That's why I would recommend to try it out for yourself and test it a little bit, and then get a feeling
for it.
But generally speaking, now you've seen the difference between using the helper parse and the helper
convert.