1 00:00:00,630 --> 00:00:01,580 Hello and welcome. 2 00:00:01,620 --> 00:00:06,030 In this lecture we are going to convert to string. 3 00:00:06,030 --> 00:00:15,070 We are also going to truncate a value so I'm going to introduce another data type called double. 4 00:00:15,160 --> 00:00:23,970 We're going to try and convert that into an integer and integers and whole number and in the process 5 00:00:23,970 --> 00:00:29,570 of converting it it will be truncated. 6 00:00:29,640 --> 00:00:30,450 Okay. 7 00:00:30,540 --> 00:00:32,300 I've got visual studio open. 8 00:00:32,330 --> 00:00:35,370 And this is where we left off in our previous lectures. 9 00:00:35,370 --> 00:00:43,100 So I'm going to I'm going to comment out this code by doing two slashes and comment out on that as well. 10 00:00:43,230 --> 00:00:50,400 So what I'm gonna do I'm going to create new variables one to create a variable and the data type will 11 00:00:50,400 --> 00:00:53,020 be double. 12 00:00:53,760 --> 00:01:06,610 I'll give it a value of k I'll set the value to three point five semicolon at the end. 13 00:01:06,660 --> 00:01:15,150 Next hour reintroduce variable I and set this value to 14 00:01:18,050 --> 00:01:25,770 I'm going to pass it an integer value or set a value to be an integer of k. 15 00:01:25,770 --> 00:01:33,150 So basically what it will do it will convert this K here value into an integer an integer is a whole 16 00:01:33,150 --> 00:01:41,670 number and in the process of doing that it will truncate truncate means because a integer does not cannot 17 00:01:41,670 --> 00:01:42,720 read decimals. 18 00:01:42,740 --> 00:01:47,300 So it will just leave the point five out and only display the three. 19 00:01:47,310 --> 00:01:49,580 So that's what truncating means. 20 00:01:49,590 --> 00:01:57,560 So next we go to to introduce our result label. 21 00:01:57,900 --> 00:02:03,500 Remember we created the result label dot text so it takes in a text value. 22 00:02:03,510 --> 00:02:07,930 So we're going to eventually make this convert this value as well. 23 00:02:08,200 --> 00:02:17,370 Now set this to equal to the variable I as you can see as a red squiggly thing on the net because it's 24 00:02:17,370 --> 00:02:27,000 complaining that that local variable is an integer we cannot implicitly implicitly convert the type 25 00:02:27,120 --> 00:02:28,910 int to his drink. 26 00:02:29,280 --> 00:02:29,790 Right. 27 00:02:29,850 --> 00:02:35,580 We can using a helper method called two strings. 28 00:02:36,000 --> 00:02:45,900 So we're going to use a helper method to do the conversion so we can make it recognize a an integer 29 00:02:46,400 --> 00:02:57,260 and the way we do this where we've got I we just get rid of the semicolon and adopt. 30 00:02:57,540 --> 00:03:01,380 That would give us some intelligence points so this with what we want. 31 00:03:01,380 --> 00:03:09,640 We want to convert it to string due to string and place the semicolon. 32 00:03:10,410 --> 00:03:10,820 All right. 33 00:03:10,820 --> 00:03:19,120 So if I save that and execute this code it should output three. 34 00:03:19,410 --> 00:03:22,050 It would truncate the point five. 35 00:03:22,050 --> 00:03:22,410 All right. 36 00:03:22,410 --> 00:03:23,370 So that's what this is. 37 00:03:23,370 --> 00:03:25,170 So let me run that click on run 38 00:03:28,410 --> 00:03:37,300 so if I click Okay issued output three you can see is output at three but left out the point five. 39 00:03:39,430 --> 00:03:50,200 So so far we've been able to convert to string using the two string method is called a helper method. 40 00:03:50,200 --> 00:03:58,060 Okay so we're going to expand on the application further using the controls we created. 41 00:03:58,060 --> 00:04:04,210 So we are going to make it possible for a user to type in a number. 42 00:04:04,210 --> 00:04:09,790 Remember we created a text ball but we're going to configured it in such a way that a user can type 43 00:04:09,790 --> 00:04:16,910 in a number and we can perform calculation based on that number and then display the result. 44 00:04:16,990 --> 00:04:17,860 Okay. 45 00:04:17,950 --> 00:04:18,280 All right. 46 00:04:18,280 --> 00:04:25,020 So we will need to perform at least two types of conversion. 47 00:04:25,090 --> 00:04:31,180 So again the text box control stores user inputs into a string. 48 00:04:31,180 --> 00:04:37,730 So we want to convert that string into an integer in order to perform a calculation on it. 49 00:04:37,900 --> 00:04:41,710 The label controls stores result as a string. 50 00:04:41,710 --> 00:04:49,570 We will want to convert that result of the integer calculation back into a string. 51 00:04:49,570 --> 00:04:50,070 OK. 52 00:04:50,080 --> 00:04:59,220 So I'm going to comment out this line of code here and introduce some other code that we can use with 53 00:04:59,220 --> 00:05:02,590 our design controls. 54 00:05:02,590 --> 00:05:02,910 OK. 55 00:05:02,950 --> 00:05:05,400 So what I wanted we want to make this controls active. 56 00:05:05,470 --> 00:05:06,210 So. 57 00:05:06,550 --> 00:05:06,880 All right. 58 00:05:06,880 --> 00:05:13,420 So I'm going to introduce some more different variables here now to save time. 59 00:05:13,420 --> 00:05:15,370 I've already written the code here. 60 00:05:15,370 --> 00:05:20,000 So I've got some code here from line 27 to 30. 61 00:05:20,170 --> 00:05:24,230 So I've created another date a string type. 62 00:05:24,550 --> 00:05:34,240 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 63 00:05:34,270 --> 00:05:41,970 a lot of variable called J which is an integer data type and I've set the value to equal to to int dot 64 00:05:41,980 --> 00:05:44,750 pass and past the value of i. 65 00:05:44,770 --> 00:05:48,140 Which is this textbook here. 66 00:05:48,640 --> 00:05:57,480 What we're doing here we're converting this string to an integer via the int dot pass. 67 00:05:57,580 --> 00:05:58,570 How permitted. 68 00:05:59,170 --> 00:06:07,780 OK so the int dot pass helper method is a bit different from the two string l permitted. 69 00:06:07,850 --> 00:06:14,290 Right we've used that earlier on this shows that the main difference is that the pass helper method 70 00:06:14,830 --> 00:06:26,220 comes from inside of the int class and takes in a string as an input in between the method parentheses. 71 00:06:26,260 --> 00:06:26,720 All right. 72 00:06:26,740 --> 00:06:32,410 So this I here is referring to this input text value. 73 00:06:32,410 --> 00:06:39,300 So with this piece of code here now the user input will be stored as an integer. 74 00:06:39,310 --> 00:06:40,220 All right. 75 00:06:40,240 --> 00:06:47,690 In the int j which is this one that will be stored as an integer and we can now perform calculations. 76 00:06:47,690 --> 00:06:48,160 All right. 77 00:06:48,160 --> 00:06:50,290 And display the result. 78 00:06:50,290 --> 00:06:54,610 True the label control which is this one here. 79 00:06:54,670 --> 00:06:54,990 All right. 80 00:06:55,060 --> 00:07:01,270 So we simply what we're doing here we're converting the K to a string. 81 00:07:01,270 --> 00:07:04,600 So let's save that and run the code. 82 00:07:04,600 --> 00:07:05,160 Okay. 83 00:07:07,570 --> 00:07:08,010 Okay. 84 00:07:08,020 --> 00:07:09,310 So this is the interface. 85 00:07:09,340 --> 00:07:17,110 If I'm typing because we've done the conversion to not be possible to type in values I type in 7 and 86 00:07:17,110 --> 00:07:17,840 click Okay. 87 00:07:17,850 --> 00:07:21,020 It should give me eight because of the code. 88 00:07:21,030 --> 00:07:31,440 There you go giving me eight typing in another value typing C6 you should give me seven. 89 00:07:31,550 --> 00:07:33,310 All right. 90 00:07:33,390 --> 00:07:37,640 The reason is adding an extra number is because of this code here. 91 00:07:37,650 --> 00:07:41,430 Line twenty nine so I've sent integer k so any value up. 92 00:07:41,460 --> 00:07:47,920 If I typed in as a K will give me the value of j plus 1. 93 00:07:48,030 --> 00:07:48,440 All right. 94 00:07:48,470 --> 00:07:53,520 So this J the integer here the pass method is doing the conversion. 95 00:07:53,520 --> 00:07:53,860 All right. 96 00:07:53,880 --> 00:07:57,960 So any thing are typing here as a J. 97 00:07:58,050 --> 00:08:00,800 It will add one to it. 98 00:08:00,810 --> 00:08:04,370 That's where we get in the the extra number from. 99 00:08:04,400 --> 00:08:10,980 So if I add for there and click okay it will give me five. 100 00:08:11,130 --> 00:08:15,940 What should happen if I type in a word for type in a word. 101 00:08:15,940 --> 00:08:19,360 Rick I should get an arrow and click Okay. 102 00:08:19,500 --> 00:08:25,750 Notice the vicious to do immediately give me an error is telling me what's going on there. 103 00:08:27,330 --> 00:08:35,250 You can see visual studio as highlighted where the problem is why it failed to at runtime is giving 104 00:08:35,250 --> 00:08:38,430 me the exception error. 105 00:08:38,430 --> 00:08:38,820 All right. 106 00:08:38,820 --> 00:08:49,830 So the problem is that the int dot pass method will only work when the string looks exactly like in 107 00:08:49,830 --> 00:08:59,160 no runtime errors are quite difficult to be caught by the compiler. 108 00:08:59,160 --> 00:09:04,290 We've seen the compiler errors before where you see read quickly bits before you actually compile your 109 00:09:04,290 --> 00:09:05,060 code. 110 00:09:05,430 --> 00:09:13,470 The runtime errors are much more difficult to catch and one of the ways to catch them is basically true 111 00:09:13,830 --> 00:09:21,270 rigorous testing and one of the things you could put in place is to perform some checks on basic checks 112 00:09:22,530 --> 00:09:30,210 to check some validation checks if a user provides set enabled input it will check the format make sure 113 00:09:30,210 --> 00:09:32,340 the input is correct or not. 114 00:09:32,340 --> 00:09:39,780 So this kind of validation will help you know address the runtime errors. 115 00:09:39,930 --> 00:09:44,320 So I'll just stop the code from running by clicking that. 116 00:09:44,490 --> 00:09:51,810 So I sit in this lecture we successfully converted integer to string. 117 00:09:51,810 --> 00:09:55,320 We also perform a truncation. 118 00:09:55,580 --> 00:09:57,290 So now set for this lecture. 119 00:09:57,300 --> 00:09:59,010 Thank you so much for watching. 120 00:09:59,010 --> 00:09:59,750 Bye for now.