1 00:00:00,360 --> 00:00:01,320 ‫Welcome back. 2 00:00:01,350 --> 00:00:07,020 ‫In this video, we're going to check out the tripods method, which allows us to convert strings into 3 00:00:07,020 --> 00:00:08,760 ‫numeric data types. 4 00:00:08,760 --> 00:00:11,700 ‫And there are multiple situations where you actually need that. 5 00:00:11,700 --> 00:00:13,110 ‫And we're going to look at one of those. 6 00:00:13,110 --> 00:00:22,530 ‫So for example, we can use the tri pass method to convert a string like 128 into a numeric data type 7 00:00:22,530 --> 00:00:23,580 ‫like an integer. 8 00:00:23,760 --> 00:00:30,180 ‫And here it's important that we have the quotation marks surrounding the 128 because this is not a number, 9 00:00:30,180 --> 00:00:31,440 ‫it's actually a string. 10 00:00:31,440 --> 00:00:32,550 ‫So it's text. 11 00:00:32,550 --> 00:00:37,320 ‫And as you know, the data type does matter in C-sharp. 12 00:00:37,800 --> 00:00:38,100 ‫All right. 13 00:00:38,340 --> 00:00:41,790 ‫Powers often gets used when the user submits input. 14 00:00:41,790 --> 00:00:47,790 ‫So for example, the user enters his name, but the data entered is a string. 15 00:00:47,790 --> 00:00:51,900 ‫So we need to convert that into a number in order to work with it. 16 00:00:52,200 --> 00:00:53,460 ‫And here's an example. 17 00:00:53,460 --> 00:01:00,840 ‫We have the main method in which we have the string called number as string, which has a value of 128. 18 00:01:00,840 --> 00:01:07,560 ‫So we need that string, of course, in order to convert that into an integer or so, in order to parse 19 00:01:07,560 --> 00:01:08,640 ‫it into an integer. 20 00:01:08,820 --> 00:01:11,700 ‫So create a variable to store the parsed value. 21 00:01:11,700 --> 00:01:19,110 ‫In this case, I'm just calling it int parsed value and then we try to parse the string and that returns 22 00:01:19,110 --> 00:01:20,220 ‫a boolean. 23 00:01:20,220 --> 00:01:27,690 ‫So we have our main method with our parsed value and we create this bool called success where we use 24 00:01:27,690 --> 00:01:29,730 ‫int dot, try parse. 25 00:01:29,730 --> 00:01:34,650 ‫So this try parse method is available within our INT data type. 26 00:01:34,650 --> 00:01:41,490 ‫We're going to look into many more methods later on which are built in in C sharp and there are thousands 27 00:01:41,490 --> 00:01:49,080 ‫of them and you can use them in order to make your code a lot more useful, practical and advanced. 28 00:01:49,080 --> 00:01:56,400 ‫And what we're doing with these tripwires is we are taking an integer in which is our number as string, 29 00:01:56,400 --> 00:02:06,810 ‫and we are storing the value that is the result of our try to parse it in the past value variable. 30 00:02:06,870 --> 00:02:16,050 ‫So the bool itself will be either true or false saying okay, it worked so I could parse or convert 31 00:02:16,050 --> 00:02:23,610 ‫the 128 string into an integer, and if it's false, then it just means that it didn't work. 32 00:02:23,610 --> 00:02:26,820 ‫So for example, there was a wrong character in there. 33 00:02:26,820 --> 00:02:32,790 ‫Let's say there was an X in there as well, or the value given was not a string or something like that. 34 00:02:33,180 --> 00:02:36,690 ‫So that's what we need is these are the steps. 35 00:02:36,690 --> 00:02:42,330 ‫And then of course we can use parsed value as an integer and that will pretty much just be the result 36 00:02:42,330 --> 00:02:45,180 ‫of our whole work that we did here. 37 00:02:45,180 --> 00:02:48,630 ‫So it will be the result of our parse approach. 38 00:02:49,020 --> 00:02:54,690 ‫We can then use this result to write something, for example, on our console. 39 00:02:54,690 --> 00:02:59,130 ‫This is a very basic example so that you can already test it yourself. 40 00:02:59,130 --> 00:03:05,580 ‫So if success is true, so our boolean, which was the result of our tripartite method, so either it 41 00:03:05,580 --> 00:03:08,160 ‫worked to parse it or it didn't work. 42 00:03:08,160 --> 00:03:14,010 ‫So here it's just going to say something like parsing successful number is and then it will just show 43 00:03:14,010 --> 00:03:19,500 ‫us the parsed value or it wasn't successful and it will just say parsing failed. 44 00:03:19,620 --> 00:03:26,430 ‫So this is then basically the structure that we're using here where we have if an LS, so if a specific 45 00:03:26,430 --> 00:03:28,470 ‫condition is met, do something. 46 00:03:28,470 --> 00:03:32,040 ‫If it's not met, do ls, so do something else. 47 00:03:32,040 --> 00:03:39,690 ‫And in this case, the ls block All right, so you can also use other numeric data types for parsing 48 00:03:39,690 --> 00:03:43,080 ‫like float or double, so you're not limited to integers. 49 00:03:43,080 --> 00:03:44,220 ‫Float, try, parse. 50 00:03:44,220 --> 00:03:48,630 ‫So you can see here in float we also have this tri parse method. 51 00:03:48,660 --> 00:03:53,400 ‫It's all the same, except that instead of integer we're now using float. 52 00:03:53,400 --> 00:03:59,370 ‫So we have float as the dating data type and we also use the float try parse method. 53 00:03:59,370 --> 00:04:05,100 ‫So here it's important that we of course adjust it to the data type that we want to look at. 54 00:04:05,100 --> 00:04:11,190 ‫So if it was successful, then it will say parsing was successful and otherwise it will just say parsing 55 00:04:11,190 --> 00:04:11,850 ‫failed. 56 00:04:11,850 --> 00:04:15,960 ‫So here, of course, this is a 128, which is a integer. 57 00:04:15,960 --> 00:04:18,300 ‫So we could of course use integer instead. 58 00:04:18,300 --> 00:04:25,440 ‫But let's have a look at examples where parsing will fail and where string cannot be converted into 59 00:04:25,440 --> 00:04:27,090 ‫a desired data type. 60 00:04:27,090 --> 00:04:29,520 ‫So for example, integer or float or double. 61 00:04:29,910 --> 00:04:33,000 ‫All right, so parsing to an end, we can see what works. 62 00:04:33,000 --> 00:04:39,960 ‫So String H 18, it's possible to convert or parse that into an integer. 63 00:04:40,680 --> 00:04:41,640 ‫However. 64 00:04:42,500 --> 00:04:48,110 ‫H equals 18 x, y, z will not be possible here. 65 00:04:48,110 --> 00:04:50,810 ‫So we cannot parse that into an integer. 66 00:04:51,680 --> 00:04:54,050 ‫And for the float here, same story. 67 00:04:54,050 --> 00:04:58,640 ‫We have 18.75 as an example and then that will of course work. 68 00:04:58,640 --> 00:05:05,720 ‫But at the same time 18.75 x, y, z will not work and it doesn't have to be x, y, z just could be 69 00:05:05,720 --> 00:05:12,200 ‫X or a or D or whatever character that just doesn't fit the structure of a float. 70 00:05:12,500 --> 00:05:12,860 ‫All right. 71 00:05:12,860 --> 00:05:17,480 ‫So I'd say let's go into practical example where we see that life in action. 72 00:05:17,480 --> 00:05:19,940 ‫And yeah, thank you for watching.