1 00:00:01,320 --> 00:00:07,950 This video is geared towards teaching you about object oriented programming and more specifically classes 2 00:00:08,040 --> 00:00:11,280 objects methods and properties. 3 00:00:11,280 --> 00:00:16,980 I've already created the project and it is called classes and objects so you can pause here and go ahead 4 00:00:16,980 --> 00:00:22,150 and create this new project under your solution and resume when you are done. 5 00:00:22,140 --> 00:00:29,670 Now let's get started with what a class is and a class is a very very important keyword when talking 6 00:00:29,670 --> 00:00:32,920 about object oriented programming languages. 7 00:00:32,940 --> 00:00:38,960 I would have used that expression multiple times and so this is the explanation. 8 00:00:39,000 --> 00:00:42,920 Now our class is a blueprint for a data type right. 9 00:00:42,930 --> 00:00:50,960 So for a bit more context we have data types we have into a string with boolean we have char right. 10 00:00:50,970 --> 00:00:57,660 And so there are times when we probably need our own data type because we need to define something instead 11 00:00:57,660 --> 00:01:04,500 of for a program that has unique properties and maybe even a mixture of those other base data types 12 00:01:04,500 --> 00:01:05,780 that I just mention. 13 00:01:05,820 --> 00:01:13,260 And so our class allows us to define our own data type which can be a combination of these other types 14 00:01:13,350 --> 00:01:16,470 and properties and methods that we want. 15 00:01:16,980 --> 00:01:22,130 All right so that was a very unofficial explanation of it and I hope it made sense. 16 00:01:22,140 --> 00:01:29,760 But even if it is still unclear as to what it is where a bullet creates a class and I want to explain 17 00:01:29,760 --> 00:01:32,550 in detail everything as we go along. 18 00:01:32,940 --> 00:01:38,510 So let's just say we wanted our own class called box no note. 19 00:01:38,550 --> 00:01:39,780 We already have a class. 20 00:01:39,810 --> 00:01:45,210 We have the keyword class that tells us well this is a class and it's called program but I want another 21 00:01:45,210 --> 00:01:53,850 class that is not programmed so I can't define a class or well it is generally discouraged to find a 22 00:01:53,850 --> 00:01:55,680 class instead of one other class. 23 00:01:55,830 --> 00:01:58,680 But I do need it inside of the namespace. 24 00:01:58,680 --> 00:02:04,060 So instead of the curly braces for my namespace I'm just going to create another class. 25 00:02:04,080 --> 00:02:12,200 Just press enter twice and then I will see class and this class is going to be called Box. 26 00:02:12,210 --> 00:02:18,530 So I want a class called Box I want a data type that looks like a box. 27 00:02:18,550 --> 00:02:20,220 No I did say it's a blueprint. 28 00:02:20,310 --> 00:02:23,640 So when you're defining a class you need to see it yourself. 29 00:02:23,640 --> 00:02:28,200 Okay I've said any class I need a new data type. 30 00:02:28,230 --> 00:02:29,700 I've given it a name. 31 00:02:29,700 --> 00:02:31,470 What does it usually look like. 32 00:02:31,470 --> 00:02:33,260 What are the properties. 33 00:02:33,270 --> 00:02:39,930 And when I say properties they are the defining characteristics of this new data type. 34 00:02:39,930 --> 00:02:47,580 Well I can tell you that a box is a 3D object generally speaking and a box usually has Length Breadth 35 00:02:47,880 --> 00:02:48,880 and Height. 36 00:02:48,990 --> 00:02:49,340 Right. 37 00:02:49,350 --> 00:02:52,850 So we know three properties that this box will have. 38 00:02:52,860 --> 00:02:58,040 So did it find properties I will need the access modifier. 39 00:02:58,140 --> 00:03:02,150 So we looked at one in when we're doing methods I would call it static. 40 00:03:02,190 --> 00:03:05,070 Well there's another one you have well public. 41 00:03:05,070 --> 00:03:06,180 You also have private. 42 00:03:06,180 --> 00:03:10,960 So in the case of a class we can define it as public. 43 00:03:11,010 --> 00:03:17,640 Then we give it to data types so the data type can be of another class or it can be off base data type. 44 00:03:17,970 --> 00:03:21,210 So once again this is me defining identities. 45 00:03:21,220 --> 00:03:25,060 So if I add another class somewhere else I could use it as a data type. 46 00:03:25,120 --> 00:03:32,940 No however I'm keeping it simple so public and then my data type for a length would probably be something 47 00:03:32,940 --> 00:03:34,070 like double. 48 00:03:34,230 --> 00:03:36,150 And then I'm going to call it length. 49 00:03:36,750 --> 00:03:40,820 Then I'm just going to say get come on sit. 50 00:03:40,850 --> 00:03:41,250 All right. 51 00:03:41,460 --> 00:03:43,380 And that's pretty much what that looks like. 52 00:03:43,380 --> 00:03:47,310 So public double length. 53 00:03:47,370 --> 00:03:52,890 And then they get there and sets are meaning this is allowing me to retrieve the value. 54 00:03:52,890 --> 00:03:55,570 And this is allowing me to set a value. 55 00:03:55,620 --> 00:04:02,410 All right so I can get the value from my property call the length and I can set a value and as he visits 56 00:04:02,480 --> 00:04:07,580 it is kind of hinting at me that you know I'm violating the naming convention so let me see what they're 57 00:04:07,590 --> 00:04:08,550 going to tell me. 58 00:04:08,610 --> 00:04:14,820 And they're just suggesting that properties off classes should get capital letters All right. 59 00:04:15,090 --> 00:04:16,240 So let's try that again. 60 00:04:16,240 --> 00:04:21,300 No this I'm going to show you a keyboard shortcuts when defining properties instead of a class in C 61 00:04:21,300 --> 00:04:29,760 sharp and Visual Studio you can write the word prop and then w top tab and then it will kind of complete 62 00:04:29,760 --> 00:04:30,750 that line for you. 63 00:04:30,780 --> 00:04:34,070 So he's going to highlight the data type I can just press. 64 00:04:34,140 --> 00:04:39,750 All right w then press tab and then move on to the next one which is my property on this one I wanted 65 00:04:39,750 --> 00:04:45,080 to see Brett and I'm using the suggested naming convention this time. 66 00:04:45,120 --> 00:04:49,150 Then I press enter and then it adds it to the property. 67 00:04:49,150 --> 00:04:56,340 All right so I want to try that again prop w tab and then double and then tab and then this one is going 68 00:04:56,340 --> 00:05:01,180 to be height for us enter and we're done all right. 69 00:05:01,180 --> 00:05:09,100 And then congratulations you just defined your first user ID find it to type or your first class. 70 00:05:09,130 --> 00:05:11,880 Know that I have this user defined data type. 71 00:05:11,890 --> 00:05:13,180 What do I do with it. 72 00:05:13,170 --> 00:05:14,620 How do I use it. 73 00:05:14,620 --> 00:05:15,250 All right. 74 00:05:15,250 --> 00:05:21,130 So let us think about how we use regular data to sort of one the regular data type. 75 00:05:21,190 --> 00:05:27,700 We just specify our data type and then give it a name and then maybe give it some value right. 76 00:05:27,700 --> 00:05:32,250 So like when we wanted a number we would've said something like int. 77 00:05:32,440 --> 00:05:36,380 That's our data type or name and then give it a value. 78 00:05:36,490 --> 00:05:37,190 True. 79 00:05:37,310 --> 00:05:46,180 No we want to define box as a variable or want a variable of type box so box is a data type. 80 00:05:46,180 --> 00:05:46,720 No. 81 00:05:46,810 --> 00:05:50,110 And we want a variable that represents a box. 82 00:05:50,110 --> 00:05:55,440 So in order to initialize a new variable or what we'd call an object. 83 00:05:55,660 --> 00:05:58,820 So I'm just going to write a comment here. 84 00:05:58,930 --> 00:06:01,210 Create object of class type. 85 00:06:01,210 --> 00:06:03,670 Box then I'm going to say something like. 86 00:06:03,670 --> 00:06:04,570 BOX. 87 00:06:04,570 --> 00:06:09,220 SO I'M JUST GOING TO CALL IT THE SAME WAY WE CALLED IT IS A C and where I'm just calling in the data 88 00:06:09,220 --> 00:06:13,060 type and then I'm going to give you 10 name someone to give it. 89 00:06:13,060 --> 00:06:19,840 Come on B box so it's very case sensitive if I define the class with I can meet with a capital B then 90 00:06:19,900 --> 00:06:22,060 it's going to be seen differently. 91 00:06:22,090 --> 00:06:28,570 Come on B but it could have been anything I could have said Box B box to be one the same naming convention 92 00:06:28,800 --> 00:06:32,960 that we would have used to go for and how we need more variables would apply. 93 00:06:32,970 --> 00:06:38,860 Here are the only exception is that they can do the case really matters a lot. 94 00:06:39,010 --> 00:06:41,420 So I have my data type box. 95 00:06:41,470 --> 00:06:44,370 I notice that it's got a color coded just like program. 96 00:06:44,380 --> 00:06:47,530 So that's a class and then box is color coded. 97 00:06:47,530 --> 00:06:54,130 Come on B box is color coded just like gnome so that's my variable are in this situation my object and 98 00:06:54,130 --> 00:07:01,450 that I'm going to initialize it to a new instance of box and this step is very important. 99 00:07:01,450 --> 00:07:08,110 All right so with all this step then well it would be it wouldn't work essentially. 100 00:07:08,140 --> 00:07:14,830 So if I just said Box B and semicolon visual studio would kind of accept it but then nothing that we 101 00:07:14,830 --> 00:07:18,310 do afterwards would work it would just throw arrows all right. 102 00:07:18,310 --> 00:07:25,560 So every time you're using a new class or a new user defined object. 103 00:07:25,660 --> 00:07:26,050 Sorry. 104 00:07:26,050 --> 00:07:33,050 A new user defined data type which is in the form of a class that you would have created. 105 00:07:33,220 --> 00:07:40,570 Then you need to initialize it by saying that class name some object name is equal to a new instance 106 00:07:40,660 --> 00:07:45,910 off that class name again and open and close braces. 107 00:07:45,910 --> 00:07:50,560 There are times you may see contents in the braces but this is not one of those times so I'm not going 108 00:07:50,560 --> 00:07:57,220 to bring up that scenario just yet and we'll just press ahead with our objective. 109 00:07:57,610 --> 00:08:02,810 So no that we have our box object or our box variable. 110 00:08:02,850 --> 00:08:05,880 Know I'm going to start calling it object from no on right. 111 00:08:05,920 --> 00:08:11,670 I keep on calling it variable sake and can run them into parallel but it's really an object wants itself 112 00:08:11,680 --> 00:08:16,040 a class type and it's no initialized it's no one object. 113 00:08:16,070 --> 00:08:23,980 All right so I can see a box and then a very very special operator that I know you've seen quite a few 114 00:08:23,980 --> 00:08:27,130 times and it probably just never will mean much to you. 115 00:08:27,130 --> 00:08:32,890 We have our full stop so box dot and then look at what the intelligence is doing. 116 00:08:32,890 --> 00:08:39,130 It's telling me that I have access to a property you see that range that range means property called 117 00:08:39,180 --> 00:08:45,910 breadth I have access to a property called height a property called length of three off which I just 118 00:08:45,910 --> 00:08:48,250 defined inside of my class. 119 00:08:48,250 --> 00:08:48,670 All right. 120 00:08:48,910 --> 00:08:52,170 So they're telling me that I can get and I can set. 121 00:08:52,180 --> 00:08:59,440 Which means that I can print box that length if I need to and I can also see a box that length is equal 122 00:08:59,440 --> 00:09:00,730 to some value. 123 00:09:00,730 --> 00:09:03,850 Someone to do that somewhere to set some values or box that length. 124 00:09:03,870 --> 00:09:16,760 So box that length is 10 and then I'm going to say box dot width or sorry breadth is equal to 15 and 125 00:09:16,760 --> 00:09:22,400 then I'm also going to say a box dot height is equal to 5. 126 00:09:22,430 --> 00:09:24,490 So I'm setting values here. 127 00:09:24,530 --> 00:09:31,320 So C sharp makes it very easy for you to create an object and then set values for these objects. 128 00:09:31,340 --> 00:09:37,780 So when I'm putting or assigning values that is me sitting when I am printing. 129 00:09:37,780 --> 00:09:43,730 That is me getting so I can actually do a console dot right line. 130 00:09:43,850 --> 00:09:45,910 All right so you see that dot operator again. 131 00:09:45,920 --> 00:09:52,520 And notice the color coding on the word console it looks just like the color coding on box and on program 132 00:09:52,790 --> 00:09:55,460 and every other class that we would call it. 133 00:09:55,490 --> 00:10:03,060 So that means the creators of the language see sharp created a class and then this class. 134 00:10:03,490 --> 00:10:08,000 And I know this because after the dot upper until the sensation of everything that I can look at in 135 00:10:08,000 --> 00:10:13,040 the class it has many properties and methods so the cube represents a method right. 136 00:10:13,310 --> 00:10:15,330 So I have a method called right. 137 00:10:15,410 --> 00:10:20,990 I also have a property called Oh it's a property called era property called this a property called that. 138 00:10:21,080 --> 00:10:22,820 My class only has three properties. 139 00:10:22,820 --> 00:10:29,480 This one has so many other properties that I may may never use in all honesty I have never used some 140 00:10:29,480 --> 00:10:34,970 of these things in here but the tools are there so I'm just trying to show you that when you're creating 141 00:10:34,970 --> 00:10:36,410 a class it is up to you. 142 00:10:36,410 --> 00:10:42,170 This is a blueprint but then it gets initialized and then everything becomes real because they can actually 143 00:10:42,470 --> 00:10:48,880 interact with the properties and methods you would have defined someone to say a console. 144 00:10:48,890 --> 00:10:49,960 That's right. 145 00:10:49,960 --> 00:10:58,420 Line and then I would just want to print out box dimensions are box length box thought breadth and box 146 00:10:58,420 --> 00:10:59,210 dot height. 147 00:10:59,210 --> 00:11:00,940 Right so I'm just printing about the volume. 148 00:11:00,950 --> 00:11:06,890 So like I said when you are trying to view the values you're really doing I get but when you're assigning 149 00:11:06,890 --> 00:11:13,190 the values you're doing I sit and look out what happens if I remove the set from length and I remove 150 00:11:13,190 --> 00:11:15,580 the get from height. 151 00:11:15,580 --> 00:11:15,980 All right. 152 00:11:16,280 --> 00:11:24,460 So now I have an arrow because I'm trying to set a value on a property that only allows me to get right. 153 00:11:24,470 --> 00:11:30,240 Yes it's public but it's seeing that you're not allowed to place a value inside this property. 154 00:11:30,250 --> 00:11:35,600 And then it's giving me that arrow because it's saying oh that's illegal it's read only you can only 155 00:11:35,780 --> 00:11:37,010 retrieve the value. 156 00:11:37,040 --> 00:11:41,140 So it's not giving me an error when I'm printing it because I'm only retrieving here. 157 00:11:41,210 --> 00:11:41,790 All right. 158 00:11:41,840 --> 00:11:44,990 And then just the same way for the the height. 159 00:11:44,990 --> 00:11:46,190 I only put it to set. 160 00:11:46,190 --> 00:11:50,540 So yes I can put a value there but I cannot retrieve it. 161 00:11:50,690 --> 00:11:51,200 All right. 162 00:11:51,200 --> 00:11:56,000 So I'm just pointing that out too because when you're going to be interacting with other classes. 163 00:11:56,110 --> 00:12:03,020 Well one when you're defining your own classes you're going to be very careful if you know if necessary 164 00:12:03,380 --> 00:12:07,460 to know what level of access you're giving to what property. 165 00:12:07,550 --> 00:12:11,390 So human being probably is. 166 00:12:11,390 --> 00:12:16,170 Well let's say you would meet any human being unless you give birth to this human being. 167 00:12:16,190 --> 00:12:19,220 You would have met them with a name so you don't just go ahead and sit. 168 00:12:19,220 --> 00:12:24,510 Name you don't set first name on set last name but you can get that information right. 169 00:12:24,530 --> 00:12:30,770 So if you were ever to define a class that maybe you call a person that looks like a human being you 170 00:12:30,770 --> 00:12:36,590 know you're defining with properties of a human being on a property of a human being would be a first 171 00:12:36,590 --> 00:12:38,750 name another property would be a last name. 172 00:12:38,750 --> 00:12:39,320 Right. 173 00:12:39,380 --> 00:12:45,340 You probably would have a first name with only get and last name we only get. 174 00:12:45,340 --> 00:12:45,650 All right. 175 00:12:45,680 --> 00:12:52,760 But then you probably have something is that can get full name and you would never probably sit sit 176 00:12:53,060 --> 00:12:57,200 put saris put us sit on the first name on the last name. 177 00:12:57,200 --> 00:13:02,750 All right so I put about my sister and might get her on my my property is in my class and I'm going 178 00:13:02,750 --> 00:13:10,810 to execute this program so let me just quickly switch over they start a program all right so a very 179 00:13:10,810 --> 00:13:16,160 simple program does spring to the box them Injuns are 10 15 and 5. 180 00:13:16,180 --> 00:13:22,180 Just like all we define them 10 15 5 and then we printed out our sentence. 181 00:13:22,210 --> 00:13:28,810 So we've been doing a lot of input output programs so you could actually modify this to receive the 182 00:13:28,810 --> 00:13:33,210 three numbers as inputs and then set the values and then do the print. 183 00:13:33,220 --> 00:13:34,200 So I'm going to do it. 184 00:13:34,210 --> 00:13:36,220 I want you to pause this video right. 185 00:13:36,220 --> 00:13:37,630 No and attempt to do yourself. 186 00:13:37,930 --> 00:13:40,920 And then you on pause and then we go through it together. 187 00:13:40,930 --> 00:13:43,420 All right so I've completed that exercise. 188 00:13:43,570 --> 00:13:46,800 So I put it in some comments and kind of rearrange the code. 189 00:13:46,900 --> 00:13:51,490 So it's always nice to have your declarations kind of one place. 190 00:13:51,570 --> 00:13:56,140 I mean I do accept that sometimes you don't know what variables you need until you get to the point 191 00:13:56,170 --> 00:14:03,430 but when you can just try and put all of them in one area and then I prompted for the three numbers 192 00:14:03,460 --> 00:14:07,530 and then I set them and then I got them. 193 00:14:07,570 --> 00:14:08,060 All right. 194 00:14:08,080 --> 00:14:11,980 So let's just run that and see how that works. 195 00:14:11,980 --> 00:14:17,460 So I'm getting the length and this is going to be a box that's 10 by 15 by a 5. 196 00:14:17,620 --> 00:14:18,760 And then it's just into print. 197 00:14:18,760 --> 00:14:21,920 The dimensions are 10 by 15 but by 5. 198 00:14:21,980 --> 00:14:22,700 No. 199 00:14:22,810 --> 00:14:25,630 What if I wanted to do something with those values. 200 00:14:25,630 --> 00:14:32,320 What if my I wanted my box to tell me its volume so I know it's length. 201 00:14:32,410 --> 00:14:33,380 I know it's breadth. 202 00:14:33,550 --> 00:14:41,620 I know it's height but then I wanted to calculate the volume based on the properties know what I can 203 00:14:41,620 --> 00:14:47,100 do is define a method so I can define a function inside of my class. 204 00:14:47,110 --> 00:14:53,080 And just for context notice we have the class called program and it has a function. 205 00:14:53,140 --> 00:14:58,540 So we went through all hold to define functions and I'm just going to use the same principles except 206 00:14:58,750 --> 00:15:02,500 this time I'm inside of a class a new class body. 207 00:15:02,500 --> 00:15:02,830 Right. 208 00:15:02,830 --> 00:15:08,320 So the losses of functions we defined them instead of the class program but I don't want these methods 209 00:15:08,320 --> 00:15:15,310 inside of program I want them inside of my class called the box and this method is going to return the 210 00:15:15,310 --> 00:15:15,730 volume. 211 00:15:15,730 --> 00:15:17,880 So I'm want to call it get volume. 212 00:15:18,020 --> 00:15:18,330 All right. 213 00:15:18,510 --> 00:15:25,720 So I want to say public and then I wanted to return a value server meal and its value returning we have 214 00:15:25,720 --> 00:15:27,720 to specify a data type. 215 00:15:27,730 --> 00:15:37,050 Some would you say double and then I'm want to call it get volume and then it is not taking any parameters. 216 00:15:37,070 --> 00:15:43,600 Now explain why it's not taking parameters it doesn't need to take parameters because it is a part of 217 00:15:43,600 --> 00:15:49,170 the class that has the properties so as as our results they're all like siblings. 218 00:15:49,180 --> 00:15:52,060 The properties are siblings than normal each other. 219 00:15:52,060 --> 00:15:57,850 The methods are siblings with the properties so you can actually just reference them directly inside 220 00:15:57,850 --> 00:15:58,750 of the method. 221 00:15:58,750 --> 00:16:03,820 You don't have to pass in any parameters because they're all inside of that box. 222 00:16:03,820 --> 00:16:07,710 All right so I mean like I said this is a data type definition. 223 00:16:07,720 --> 00:16:15,010 So if I have a box that says a length with a height it's also as I value all of those are still related 224 00:16:15,010 --> 00:16:16,590 to this one box. 225 00:16:16,660 --> 00:16:22,090 So you can think of a ladder you define the class you gave it properties maybe you gave it a method 226 00:16:22,330 --> 00:16:29,080 or two functions whatever it is but they are all a part of any one object of this box. 227 00:16:29,080 --> 00:16:29,350 All right. 228 00:16:29,590 --> 00:16:35,700 So I'm going to say and then notice the red line because its value returned so I need to return. 229 00:16:36,070 --> 00:16:43,470 And then I'm returning length times red times height. 230 00:16:43,570 --> 00:16:49,120 All right so that's so we get the volume of any 3D objects. 231 00:16:49,600 --> 00:16:59,260 So no that I have this value I can go back to my main phone Sean or let me put it in the area where 232 00:16:59,260 --> 00:17:06,190 I'm getting the properties and then I'm just going to see it volume so I did say it's good to declare 233 00:17:06,190 --> 00:17:12,930 variables in one area on this you you realize you needed somewhere else say it as they cleared so into 234 00:17:12,940 --> 00:17:19,750 volume is going to be equal to our object call box when I said Dot you're going to notice that I have 235 00:17:19,750 --> 00:17:22,630 get volume no available to me. 236 00:17:22,630 --> 00:17:26,380 So when I say get volume it will meet that function call. 237 00:17:26,410 --> 00:17:34,000 So when it's executing it gets the values from our users and then it sets the values and then we get 238 00:17:34,000 --> 00:17:41,470 the volume but then notice of an error here because it's and I am returning a double but I'm storing 239 00:17:41,470 --> 00:17:49,990 it inside of an int so bad that needs to be double volume is equal to get volume and then what I'm going 240 00:17:49,990 --> 00:18:01,260 to do is say box volume is and then I'm just printing back the value that is inside of my variable called 241 00:18:01,650 --> 00:18:02,950 volume. 242 00:18:02,970 --> 00:18:06,040 All right so let's go through this again. 243 00:18:06,050 --> 00:18:09,890 We have a box we declared a data object called box. 244 00:18:10,010 --> 00:18:16,630 We're filling it with values and then we're calculating a volume based on those values no look I'll 245 00:18:16,640 --> 00:18:22,550 need this code looks imagine if you were to write all of those things one place and it can get messy 246 00:18:22,550 --> 00:18:31,790 so inside of your class you can have as many functions I could even see public maybe I have another 247 00:18:31,790 --> 00:18:41,150 function that gets the area so I'm going to say get area this time of any one side and then area is 248 00:18:41,150 --> 00:18:44,920 usually length by breadth so it's not the heights right. 249 00:18:44,960 --> 00:18:55,350 So I have another function so I can do another one where I see double area is equal to box dot get area. 250 00:18:55,460 --> 00:19:03,540 So as many functions as I will need or want I can define them inside of my class and then on on you 251 00:19:03,540 --> 00:19:12,110 know on a need need by need basis I just declare my object made my function call and then get the value 252 00:19:12,440 --> 00:19:14,460 and you can have void functions also. 253 00:19:14,460 --> 00:19:20,940 So it's not always value returns I'm just going to duplicate this line using control D and print out 254 00:19:20,990 --> 00:19:27,320 the area that's I'm getting back all right so let's try and run this program and I'm going to do something 255 00:19:27,320 --> 00:19:33,910 that I know is going to throw an arrow noticed that I carelessly have been using double. 256 00:19:34,010 --> 00:19:38,880 So I'm in suggesting that my length breadth and height can be double. 257 00:19:38,900 --> 00:19:42,680 However I am only doing an inch that pass to the Reid line. 258 00:19:42,710 --> 00:19:44,060 So this is the arrow. 259 00:19:44,180 --> 00:19:53,000 If my length was one point five and I press enter then I'm getting an on handled exception error. 260 00:19:53,000 --> 00:19:53,350 All right. 261 00:19:53,360 --> 00:19:58,670 So we did talk about the exception handling where it's good to wrap it up in the try catch to avoid 262 00:19:58,670 --> 00:19:59,680 these kinds of errors. 263 00:19:59,930 --> 00:20:05,360 But just for simplicity's sake I'm just going to choose this I'll say if we have inquires fires then 264 00:20:05,360 --> 00:20:06,840 guess what we also have. 265 00:20:06,920 --> 00:20:15,430 We have double dot pass art and if we're doing a double dot pass we also need to have the value that 266 00:20:15,430 --> 00:20:17,110 is being passed going into a double. 267 00:20:17,410 --> 00:20:17,740 All right. 268 00:20:17,740 --> 00:20:19,840 So let's just changes all to doubles. 269 00:20:19,900 --> 00:20:21,720 Like I said is very strict. 270 00:20:21,880 --> 00:20:24,270 And then we just change them all to double. 271 00:20:24,280 --> 00:20:29,630 So we have double anyone doing a double pass and double double double double double. 272 00:20:29,680 --> 00:20:33,200 And everything is no relative to doubles. 273 00:20:33,250 --> 00:20:43,490 So let's try this again so the length is one point five and the breadth is three point four and the 274 00:20:43,640 --> 00:20:45,000 height is nine. 275 00:20:45,050 --> 00:20:46,930 Just some random dimensions. 276 00:20:46,940 --> 00:20:53,780 So now I'm seeing that the dimensions are those three values the area are the volume sorry is forty 277 00:20:53,780 --> 00:20:54,860 five point nine. 278 00:20:54,860 --> 00:20:57,810 So that's one point five times three point four times nine. 279 00:20:57,980 --> 00:21:04,490 And then the area is five point one because that's one point five times three point four. 280 00:21:04,490 --> 00:21:09,110 So now we have some data tab that represents a box. 281 00:21:09,260 --> 00:21:09,950 Well guess what. 282 00:21:09,950 --> 00:21:14,750 What if we had two boxes or more than one boxes or we had a collection of boxes. 283 00:21:14,750 --> 00:21:18,890 The fact is that we can define as many boxes as we need. 284 00:21:18,890 --> 00:21:21,220 So I'm going to say this is box. 285 00:21:21,410 --> 00:21:28,150 And then there's a second box that I need to be aware of for work or dimension as far as I can see. 286 00:21:28,160 --> 00:21:30,730 Box do. 287 00:21:31,250 --> 00:21:32,480 So there is nothing stopping me. 288 00:21:32,660 --> 00:21:37,890 It's just that all we can have multiple integer variables multiple string variables. 289 00:21:37,940 --> 00:21:43,320 You can have multiple objects and misspellings defeating me. 290 00:21:43,370 --> 00:21:46,120 You can have multiple objects of the same data type. 291 00:21:46,130 --> 00:21:48,510 As long as the names are not the same. 292 00:21:48,550 --> 00:21:49,140 Right. 293 00:21:49,190 --> 00:21:53,580 So I can have box box to box five box ten. 294 00:21:53,600 --> 00:21:53,930 No. 295 00:21:53,930 --> 00:21:59,470 Imagine trying to fill in the dimensions of a warehouse full of boxes. 296 00:21:59,570 --> 00:22:04,700 So you know that there are 10 boxes and you have to write a program to allow them to enter the dimensions 297 00:22:04,700 --> 00:22:05,540 for 10 boxes. 298 00:22:05,540 --> 00:22:12,590 Then you're going to have box box to box three box for a box five and then you went off problems per 299 00:22:12,590 --> 00:22:19,280 box because then for Box 2 and went to have to repeat all of these steps just to make sure that I'm 300 00:22:19,280 --> 00:22:20,990 getting 4 box 2. 301 00:22:21,140 --> 00:22:22,660 So there is an easier way. 302 00:22:22,670 --> 00:22:30,740 I'm just giving an idea of how tedious it might get but I am just saying that that is what at this point 303 00:22:30,740 --> 00:22:32,020 we would have to do. 304 00:22:32,050 --> 00:22:34,430 So I'm just going to proceed with Box 2. 305 00:22:34,430 --> 00:22:42,250 So we have the promise for the vies for the box dimensions and we're sitting and printing the values 306 00:22:42,260 --> 00:22:45,260 already sitting and calculating the values for our box. 307 00:22:45,290 --> 00:22:53,280 So I'm just going to duplicate all of this and I'm going to do it again underneath. 308 00:22:53,460 --> 00:22:57,170 Right but then this time I'm going to change these zones. 309 00:22:57,170 --> 00:23:03,470 I'm not going to read it clear in 1 and 2 and 3 so like I said if you have to reuse a variable you can 310 00:23:03,470 --> 00:23:06,920 actually just forensic by name after it has been declared right. 311 00:23:06,920 --> 00:23:08,090 So we declared it here. 312 00:23:08,180 --> 00:23:09,370 We're not declaring target. 313 00:23:09,380 --> 00:23:10,520 We're just using it again. 314 00:23:10,520 --> 00:23:16,540 So I'm just making reference to it and then I'm changing these old to box one. 315 00:23:16,550 --> 00:23:19,900 So it's is it Box 1 box 2 Sorry. 316 00:23:19,940 --> 00:23:26,840 So we're changing these references out to Box 2 and then once I'm done this I'll just explain quickly 317 00:23:26,840 --> 00:23:39,680 exactly what is happening here and then I'm going to duplicate all these print lines we're all right. 318 00:23:41,680 --> 00:23:50,240 Where I'm going to be printing bucks to dimensions and then notes I'm going to have to overwrite. 319 00:23:50,270 --> 00:23:55,070 So I'm not going to do an override what I'm going to show you that you can actually print directly from 320 00:23:55,130 --> 00:23:55,880 our return. 321 00:23:55,880 --> 00:23:56,640 Right. 322 00:23:56,690 --> 00:24:03,830 So open to know what I've suggested that you put the return value sermon but I get volume is a function 323 00:24:04,190 --> 00:24:08,150 that returns something so in it returns that value. 324 00:24:08,240 --> 00:24:14,180 I notice I didn't use a variable this time I just returned the result of this calculation and in that 325 00:24:14,180 --> 00:24:20,060 result of the calculation is going to get stored inside volume no don't hear I'm printing volume and 326 00:24:20,060 --> 00:24:26,900 I'm printing era which we know are for box the first box I went to remove those declarations for the 327 00:24:26,900 --> 00:24:35,840 second box so all I'm sitting is the height the breadth and the length but then I'm printing box to 328 00:24:35,960 --> 00:24:43,300 dots and then I'm just going to call a function right there get volume and then I'm going to print again. 329 00:24:43,430 --> 00:24:45,170 Box dots. 330 00:24:45,290 --> 00:24:45,610 Sorry. 331 00:24:45,620 --> 00:24:51,360 Box 2 dot get area. 332 00:24:51,370 --> 00:24:52,100 All right. 333 00:24:52,150 --> 00:24:59,060 So then I'm just showing you that you can print the variable you can do the return call get it storage 334 00:24:59,080 --> 00:25:04,560 in a variable and then manipulate the variable which might be more desirable if you have you know all 335 00:25:04,600 --> 00:25:10,450 the things you need to do with the value but then you can also just return it and have it print directly 336 00:25:10,450 --> 00:25:13,980 inside off your print operation. 337 00:25:13,990 --> 00:25:14,750 Right. 338 00:25:14,800 --> 00:25:16,200 So you have those two. 339 00:25:16,240 --> 00:25:19,360 And of course you have to know when you're using which method. 340 00:25:19,390 --> 00:25:24,260 So let's run this program once again where have two boxes. 341 00:25:24,280 --> 00:25:29,920 So have two boxes of of the data type that I just created called box. 342 00:25:30,020 --> 00:25:37,810 I'm going to print prompts to enter values for the first box and then I'm going to calculate the volume 343 00:25:37,810 --> 00:25:42,880 and the area that I want to prompt again to enter values for the second book. 344 00:25:42,880 --> 00:25:49,750 So I mean I can modify the prompt to say four box 1. 345 00:25:49,820 --> 00:25:57,270 All right so I'll just do that and then repeat that for the problems for Box 2 and then after getting 346 00:25:57,270 --> 00:26:03,300 the virus for Box 2 we're just sitting the dimensions accordingly and then we're just going to print 347 00:26:03,540 --> 00:26:12,290 box one dimension box one area box one volume and then box two box two and box. 348 00:26:12,370 --> 00:26:17,070 So I'm just adding these so that we have some visual cues as to which values our full box one and which 349 00:26:17,070 --> 00:26:18,270 one is our full box. 350 00:26:18,300 --> 00:26:22,980 So let's run this and see how that works. 351 00:26:22,990 --> 00:26:28,950 All right so I've already entered some of the values 5 6 and 3 4 dimensions for box 1 and then I want 352 00:26:28,950 --> 00:26:36,780 to see if 5 4 and 10 4 dimensions will box to press enter and then we can see here Box 1 dimensions 353 00:26:36,780 --> 00:26:44,280 are these 3 and then Box 2 dimensions are those 3 and those are the calculations issue that I'm still 354 00:26:44,280 --> 00:26:50,340 getting the area and the volume even though I'm just printing directly into the console or at line the 355 00:26:50,400 --> 00:26:51,720 return value. 356 00:26:51,720 --> 00:26:52,080 All right. 357 00:26:52,110 --> 00:26:59,280 So make sure don't get confused by the curly brace and that that we're making a function call instead 358 00:26:59,280 --> 00:27:00,960 of one sold out right line here. 359 00:27:00,990 --> 00:27:07,020 So when it gets to this line and it starts writing the text you realize is that all I have to make a 360 00:27:07,020 --> 00:27:07,970 function call. 361 00:27:08,190 --> 00:27:14,880 Let me go to the function and then that's going to return some value and then whatever it returns is 362 00:27:14,880 --> 00:27:23,040 what gets concatenated into the string that is being written to the console so like I keep on saying 363 00:27:23,040 --> 00:27:24,700 there are many ways to do things. 364 00:27:24,720 --> 00:27:31,500 I think that you know getting it back answering it in a variable is much cleaner much more much easier 365 00:27:31,500 --> 00:27:34,420 to read than something looking like this. 366 00:27:34,420 --> 00:27:40,820 But once again you have that method available to you know as a final item when to display it to you. 367 00:27:40,890 --> 00:27:43,050 The best way to handle your classes. 368 00:27:43,080 --> 00:27:51,210 So you would have had to go through the painstaking explanation of of seeing why we should not define 369 00:27:51,270 --> 00:27:53,310 another class instead of this class. 370 00:27:53,340 --> 00:27:53,610 Right. 371 00:27:53,640 --> 00:27:59,730 So we have class program and to go through you know quite a bit of record restructuring to make sure 372 00:27:59,730 --> 00:28:01,240 that I was not inside. 373 00:28:01,250 --> 00:28:04,500 It's fairly obvious what's inside of the namespace brace. 374 00:28:04,680 --> 00:28:13,200 And it is actually best practice it leads to much cleaner looking project files when I go to court and 375 00:28:13,200 --> 00:28:21,420 just court in general when you create a new file per class file so notice that we have a class called 376 00:28:21,420 --> 00:28:22,270 program. 377 00:28:22,320 --> 00:28:26,040 We have a corresponding file in our project called program. 378 00:28:26,090 --> 00:28:26,720 Yes. 379 00:28:26,790 --> 00:28:33,090 Which then means any class that you create it is best practice to create a new file. 380 00:28:33,090 --> 00:28:39,780 Call it that class name dot see s and please your definition of a class inside that file. 381 00:28:40,020 --> 00:28:41,700 So I'm actually going to do that right. 382 00:28:41,700 --> 00:28:47,010 No someone to take this class that we just created and I'm going to cut it. 383 00:28:47,040 --> 00:28:52,740 So all of the code associated with the class I'm just going to do control X and I'm cutting it from 384 00:28:52,740 --> 00:28:53,660 inside me. 385 00:28:53,880 --> 00:28:58,080 And then we if we give visual studio a few seconds and everything is going to go haywire it's going 386 00:28:58,080 --> 00:29:03,340 to be low definition for a bit the type period that doesn't exist. 387 00:29:04,380 --> 00:29:08,100 And then we're going to see a visual studio it's fine we know what we're doing so we're going to go 388 00:29:08,100 --> 00:29:14,230 over to our project classes on objects right click and then click add. 389 00:29:15,030 --> 00:29:19,170 And then what we're doing is adding a new class you see that class. 390 00:29:19,330 --> 00:29:20,870 So we click us. 391 00:29:20,970 --> 00:29:26,870 And then once again whatever the name of the class is it is best practice to give the file that name 392 00:29:26,870 --> 00:29:29,760 someone to call this glass box. 393 00:29:29,860 --> 00:29:36,880 So then it's going to go ahead and create a new file and it's going to even go as far as filling in 394 00:29:36,880 --> 00:29:42,040 the name of the class the box because contextually it's already naming the class. 395 00:29:42,070 --> 00:29:48,490 So I'm just going to replace this predefined code and notice it seeing for references and that's because 396 00:29:49,240 --> 00:29:56,980 just by me creating that class and box not properties are anything in my mean it automatically takes 397 00:29:56,980 --> 00:30:03,280 that in the data type is box so I know what I'm doing but then it has no properties it has no function. 398 00:30:03,280 --> 00:30:05,260 So everything is still haywire. 399 00:30:05,350 --> 00:30:11,020 So I'm just going to replace this block of code with the class that I just created. 400 00:30:11,020 --> 00:30:16,000 And then when I go back over to my main then everything is fine. 401 00:30:16,000 --> 00:30:22,600 So once again when we're able to create the class is actually better to just go to our project right 402 00:30:22,600 --> 00:30:30,920 click go to add click class your class name so let's say I created a class called Person. 403 00:30:30,940 --> 00:30:31,350 All right. 404 00:30:31,360 --> 00:30:35,530 And then do all of our definitions all of our properties. 405 00:30:35,530 --> 00:30:40,390 And I just filled the three properties quickly I didn't want to bore you with writing them so you can 406 00:30:40,390 --> 00:30:44,980 pause and write them off if you need to but their first name last name and age. 407 00:30:44,980 --> 00:30:51,730 And then I'm going to declare another one and this time I'm using a different access modifier and this 408 00:30:51,730 --> 00:30:54,010 one is going to be called private. 409 00:30:54,010 --> 00:30:58,450 So private would be celery for argument's sake. 410 00:30:58,490 --> 00:31:02,370 So stubble and SA right. 411 00:31:02,410 --> 00:31:03,020 All right. 412 00:31:03,350 --> 00:31:09,270 So I'm defining these as public and these as private. 413 00:31:09,280 --> 00:31:15,400 Know when something is private it means that nothing outside of the class can access it you cannot directly 414 00:31:15,400 --> 00:31:18,730 access this property from the object. 415 00:31:18,730 --> 00:31:19,060 All right. 416 00:31:19,060 --> 00:31:21,280 So let me just put that into context for it. 417 00:31:21,280 --> 00:31:22,980 So I want to go back to program. 418 00:31:23,020 --> 00:31:28,440 I'm not going to do too many modifications because person is coming on the tail end of this lesson. 419 00:31:28,570 --> 00:31:35,620 But let's just see what happens someone to say a person I notice it takes person because it's in a class 420 00:31:35,620 --> 00:31:40,240 file it's in the same namespace so they know about each other and their siblings. 421 00:31:40,240 --> 00:31:48,550 So person and then as usual I have to declare a new object off person someone to say capital P person 422 00:31:48,560 --> 00:31:50,080 that's the name of the class. 423 00:31:50,110 --> 00:31:56,290 Come on peepers and that's the name of the object is equal to a new instance of the person. 424 00:31:56,290 --> 00:31:56,670 All right. 425 00:31:57,100 --> 00:32:05,140 So I can say a person the object and say dot and then in tennis consistently me exactly what I can access 426 00:32:05,170 --> 00:32:10,810 I can access the age because that was public I can access the first name and last name because those 427 00:32:10,810 --> 00:32:12,100 two are also public. 428 00:32:12,190 --> 00:32:16,180 But I cannot access the salary because it is private. 429 00:32:16,180 --> 00:32:16,540 Right. 430 00:32:16,780 --> 00:32:23,860 So if I go as far as writing salary because I'm defying visas through the N C sharp I I'm going to say 431 00:32:23,860 --> 00:32:24,730 right. 432 00:32:24,820 --> 00:32:32,560 And I want to see salary then is going to say that it cannot it is not accessible because of its protection 433 00:32:32,560 --> 00:32:33,050 level. 434 00:32:33,050 --> 00:32:34,540 So because I said it's private. 435 00:32:34,540 --> 00:32:36,130 That's private information. 436 00:32:36,130 --> 00:32:39,830 I can just go on and say I want to see salary. 437 00:32:39,850 --> 00:32:40,150 All right. 438 00:32:40,150 --> 00:32:51,820 So usually when you have private properties or a private member feels inside of a class then you mean 439 00:32:51,880 --> 00:32:54,040 what you call get their function. 440 00:32:54,040 --> 00:32:58,480 So where are already defined that we can have a default get a default set. 441 00:32:58,480 --> 00:33:02,750 Once we define our public property. 442 00:33:02,800 --> 00:33:06,520 But then when it's private you probably don't want to do it like that. 443 00:33:06,520 --> 00:33:10,410 So you can't actually do something like defining a function. 444 00:33:10,420 --> 00:33:20,810 I'm just going to call this public and call it double and then when to say get salary and then this 445 00:33:21,410 --> 00:33:31,820 public function that I should be able to access when I say person dot will return salary so return salary. 446 00:33:31,810 --> 00:33:32,780 All right. 447 00:33:32,930 --> 00:33:36,550 And then what if I needed to set the value for a salary. 448 00:33:36,560 --> 00:33:42,860 Remember it's private and it doesn't have any getter and setter so usually they would actually create 449 00:33:42,860 --> 00:33:46,030 another function and they call this one set. 450 00:33:46,070 --> 00:33:51,630 So you can see public double get salary and this is going to return. 451 00:33:51,680 --> 00:33:56,420 But then this one can be void because I don't need to return anything when I'm sitting the salary. 452 00:33:56,420 --> 00:34:00,950 However I'm going to require that you tell me home What salary I'm setting for this person. 453 00:34:00,980 --> 00:34:03,270 So I need a value from use. 454 00:34:03,350 --> 00:34:04,220 I want to see. 455 00:34:04,220 --> 00:34:09,150 Send me a parameter and I'm going to say give me salary. 456 00:34:09,260 --> 00:34:12,600 Come on s and then no return. 457 00:34:12,600 --> 00:34:21,290 But I'm sitting the capital S salary which is my private member field to have the same value as the 458 00:34:21,290 --> 00:34:25,040 salary that is coming in through the parameters. 459 00:34:25,070 --> 00:34:27,650 So salaries equals a salary. 460 00:34:27,740 --> 00:34:35,270 So a good naming conviction actually when it comes to private members is that you use on the score and 461 00:34:35,270 --> 00:34:36,670 not capital letters. 462 00:34:36,700 --> 00:34:43,160 So the public accessible ones would have the capital letter but once it's private to use an underscore 463 00:34:43,190 --> 00:34:44,230 and come on. 464 00:34:44,270 --> 00:34:48,920 So let me just change that's out and I mean you saw that it worked either way. 465 00:34:48,950 --> 00:34:53,260 So it's not so much of it's working versus not working is just a matter of. 466 00:34:53,270 --> 00:34:58,300 This is the standard so somebody else comes along looks out to your code they know exactly what they're 467 00:34:58,310 --> 00:35:00,180 looking at at a glance. 468 00:35:00,180 --> 00:35:00,910 All right. 469 00:35:00,980 --> 00:35:07,850 So private double salary that his or one private member field and we need a function to get the value 470 00:35:07,850 --> 00:35:11,580 that is there how we need a function to sit of value in there. 471 00:35:11,630 --> 00:35:21,770 So back in the program I can see a person Dot and I will only have access to any methods or properties 472 00:35:21,770 --> 00:35:34,860 that have the public identifier or access modifier sorry of indicator on their declaration 473 00:35:44,010 --> 00:35:46,530 so that's it for this very very 474 00:35:54,080 --> 00:35:58,880 so that's it for this quick introduction to classes and objects in C sharp. 475 00:35:58,880 --> 00:36:01,190 I'm going to encourage you to just think of anything. 476 00:36:01,190 --> 00:36:08,480 Think of a pit and then define a class called pit and then start writing all the different properties. 477 00:36:08,480 --> 00:36:16,250 And I like to think of the properties as the knowns and the methods as the verb right so the known would 478 00:36:16,250 --> 00:36:18,510 be like something that is there. 479 00:36:18,650 --> 00:36:21,950 So a box is always wins out but then it's always lands after birth. 480 00:36:22,010 --> 00:36:28,250 It's always good to have a height but then getting the volume and getting the area or in the air in 481 00:36:28,250 --> 00:36:35,750 the student of the person getting the salary or setting the salary or even getting full name. 482 00:36:35,750 --> 00:36:37,150 So let me just type that one. 483 00:36:37,170 --> 00:36:40,930 I want you to complete this string. 484 00:36:40,970 --> 00:36:42,410 Public string. 485 00:36:42,410 --> 00:36:44,440 Get full name. 486 00:36:44,900 --> 00:36:53,210 So I want you to define this function to get this person's full name and I want you to share your code 487 00:36:53,210 --> 00:36:53,790 with me. 488 00:36:53,900 --> 00:36:57,380 And then we can go through any difficulty you had together. 489 00:36:57,380 --> 00:37:02,270 So once again the methods are more like verbs because this is an action getting the full name is an 490 00:37:02,300 --> 00:37:04,280 action but the name is always there. 491 00:37:04,280 --> 00:37:05,920 That's more like unknown. 492 00:37:06,010 --> 00:37:08,470 All right so that's a challenge for you. 493 00:37:08,720 --> 00:37:09,920 Let me know if you have any.