‫Welcome back.
‫In this video, we are going to keep on going with properties and setters and getters.
‫We started off on the last video where we looked at setters and getters and now we're going to use the
‫same code and we're going to extend it a little bit.
‫So we saw that we can use setters in order to set the value of a member variable and we can implement
‫our own logic how we want to set it.
‫So we can say, okay, if you want to set the value, it has to follow my rules so we can define our
‫own rules, which have to be.
‫Followed.
‫And then we have getters which we can also adjust.
‫So we can just say, okay, if you want to get the variable I'm going to give you in this case, the
‫result of the multiplication of length, height and width so you can modify them to your liking.
‫Now let's have a look at properties and how this is all connected with each other.
‫So let's create a property and I'm going to create a public property called height, and this height
‫will adjust my height member variable.
‫So it was public before.
‫Now I'm going to make it private so that we cannot access height from outside.
‫So here, if I want to adjust height, I will not be able to do it any more.
‫Now I get this error.
‫All right?
‫Now let's use our height property in order to still be able to access our height up there.
‫So how do we do that?
‫Well, we, first of all, use this property with a capital H at the beginning.
‫That's generally how you use properties.
‫That's a convention and C programming.
‫The properties start with the capital letter and now here I can create my getter and I can return the
‫height and I can create my setter.
‫And I can just set the height here to being, for example, the value that is assigned.
‫Now, what is this value keyword?
‫Well, what we're doing here is basically the same thing as we did before, where we set length and
‫get length.
‫But now our gateway, so to speak, is our property height.
‫So if you want to access or change the height memorable, you have to do it via the property height.
‫So now what you can do is you can set it at the value that you set will be set to the height.
‫Now how does it actually look like when we go to our program now here if we try to set the height?
‫By accessing the member variable directly, we cannot do it anymore.
‫But if you access the property, as you can see, the height is available here and it's this tool icon
‫that we can see here.
‫So this is generally the icon that you have when you want to access a property and you can see this
‫works.
‫So we can set the height now and if we do so, this will or this for here, whatever is behind this
‫equal sign or after this equals sign, that will be the value here, this value and it will be set as
‫the height.
‫So this value is pretty much the same thing as length as we had before when we created this set length.
‫It's the same concept pretty much, but we it's just a shorter way of doing it, right?
‫So here you can see if we wanted to have a setter, we had all of this code.
‫Of course, we didn't need this code here.
‫All right?
‫But still, we had all of this code and now we have this super short code.
‫So this is the setter.
‫Of course here we can also adjust it.
‫Now, let's say if value is less than zero, then we will set our height to be minus value and else
‫it's going to be height, equal value.
‫Now let's go to our program and I'll set the height to minus four.
‫And at length that said that before because we created this error before.
‫Right.
‫And now let's set the height to minus four and let's run our code.
‫And we will see that it works.
‫So length is for height is for width is five.
‫So why is that?
‫Even though we set the value to minus four?
‫Well, we said if the value of the height that you pass to me is negative, then I'm going to multiply
‫it.
‫So if the value is less than zero, I'm going to multiply it with minus one.
‫So I'm gonna make it positive again.
‫And if it's positive, then just make it or assign the value.
‫Or if it's zero, it's fine as well.
‫All right.
‫So that is what we can do with properties.
‫Now, this is an implementation of a property.
‫Of course, what you can do is you can also just use the property without having a member variable.
‫So this member variable that we have here could be something that is not required whatsoever.
‫So we could just even skip that.
‫So let's say I don't want to set the width as a member variable, I want to have it as a property.
‫And now I'm going to show you another way to create a property.
‫And this is a lot, lot faster.
‫So you just use P, R, o, p, and then you double tap and this will create this property for you.
‫Now you just can enter the type and I'm going to say, okay, it's going to be an integer.
‫Then you press tab again and now you can give it a name.
‫So I'm going to call this one width.
‫Now what is going on here?
‫Well, this is a code that is just understood by C sharp and it even was created for us automatically
‫with our ID.
‫Right.
‫It just says get semicolon set semicolon.
‫So this just means that it's just going to be a default getter and a default setter, which just means,
‫okay, if you want to access width, you can just access it by using the width variable this property.
‫And if you want to set it, you can also just use it.
‫So what does it mean in our case?
‫Well, it means that now we can access this property by just assigning a value.
‫And if we want to write it out.
‫So if we want to access it, boxes with is box dot.
‫With.
‫So here we can access the property directly.
‫All right.
‫So this is the difference here.
‫So internally it's the same thing as if we created.
‫The.
‫With.
‫Let's get back to this with let's have this member variable again.
‫So internally it's the same thing as if I created this public end with and I'm going to comment this
‫out for now and I have a getter so get I return the width.
‫Or this dot with.
‫And the setter where I said they start with with value.
‫All right.
‫So this is really.
‫This.
‫Code here is really just the same thing as this one.
‫And internally, it's also what happens once you compile the code.
‫So the compiler does that automatically.
‫For us it just says, okay, if you have this line of code, I'm going to internally treat it as if
‫it was this code that you have here, only that it's a lot shorter and it's a lot easier to create this
‫code.
‫All right.
‫So I'm just going to comment this out because I really like this short approach that we have there,
‫and let's comment this out like this.
‫And I don't need this member variable anymore.
‫All right.
‫Because here we needed to remember variable because I set the width and in this case, I set the member
‫variable width.
‫I didn't set the.
‫I cannot cannot set the width itself.
‫So if I instead here would write with.
‫And with if I now do that, what I create as a StackOverflow.
‫So I constantly try to set something and it's going to be an endless loop here, which is not good.
‫That's why I needed to remember variable for this example here.
‫All right.
‫But internally, it's going to treat it like this, like that.
‫So it's internally treating it like the code that we have here.
‫Only that it's a lot shorter for us.
‫All right.
‫So now a little challenge for you.
‫Please go ahead and create a property for volume which surrounds it like we did with the width here.
‫All right.
‫So pretty much the same example that we had here, only that it's going to adjust the volume and not
‫the width.
‫So please try that.
‫Create your own property, type it out.
‫Don't make the short approach or use a short approach.
‫All right.
‫So for this to make sense, we first of all have to make our volume private and then we can create a
‫property for our volume.
‫So public int volume with a capital V is going to be get.
‫Return volume and then set.
‫This start volume is equal to.
‫Value.
‫So this is the property that we created for the volume.
‫I hope you were able to do so.
‫Of course, you just could have gotten rid of this volume altogether and created it the same way that
‫we did with the width.
‫It's a lot quicker, but I just wanted to show you that this is all so cool.
‫Now, the.
‫Important part hereby is that we can think about the volume variable or this property and why, or at
‫which point does it make sense to change it or get it?
‫Does it even make sense to do both of them?
‫Because the volume is something that is calculated based on the width and height and length.
‫Now, does it make sense to set the volume directly?
‫Well, no, I would say so.
‫I think setting the volume doesn't even make sense at all so we could get rid of this whole setter and
‫only have a getter.
‫Now, the volume itself, we never set the volume.
‫It's always calculated.
‫So does it make sense to just return the volume?
‫Well, it really doesn't, because actually what we should do is here at this point, we should calculate
‫that the volume is going to be the multiplication of length, height and width, you see.
‫So now it's a lot more meaningful.
‫So what we return with the volume is a lot more meaningful.
‫If somebody wants to get the volume, that's what they're getting.
‫Now, of course, we created a little bug here at the bottom or some get volume methods that we don't
‫need anymore.
‫And at the same time here, the width now is the property that we access.
‫It's not going to be the.
‫Remember variable anymore because we don't have it anymore.
‫And of course, in our program CSS, we can now also adjust everything.
‫So now we can access the width directly and we can access the volume property directly and not the volume
‫member variable anymore.
‫All right.
‫So now let's do a final step here.
‫And that is to add a constructor.
‫So public.
‫Box.
‫INT length.
‫INT.
‫Height and end.
‫Width.
‫So now if we create a box, I want to have all of those values and the length that is given to me once
‫the box is created should be this dot length.
‫Then the height that is given, this dot height should be the height.
‫So the height that is given this one should be the height that we have here, which is our member variable.
‫And our width is the final one and this is the property.
‫So we set it to the width that is given to us.
‫Now, in this case, you might of course omit this, this keyword.
‫You can just say width, because it's also obvious that we are talking about the property of this class.
‫So you don't need this this keyword here.
‫Now, an important thing that somebody asked and I wanted to really make this clear.
‫So this dot length, equal length works, but length is equal to this dot length does not make sense.
‫So this line here does not make sense because length is this part here, which is really just the parameter
‫and it's the value that is given to us.
‫So we don't want to adjust the value that is given to us, but we want to set the value of our property
‫or of our member variable, and that's what we do here.
‫So in this case, that's what we do.
‫We set the value of our member variable.
‫All right, now let's use this box.
‫Let's create boxes now, because now we don't need to do all of that anymore.
‫We don't need to set the values directly anymore, but we can set them in the creation of our box.
‫So I'm just going to assign values three, four and five.
‫So the length is three.
‫The height or actually the height is four into width is five.
‫And now if we run this, it will display the width and the volume for us.
‫So there you are with this.
‫Five volume is 60.
‫All right.
‫That seems to work.
‫Now, what we can do is we can set the width by accessing our property.
‫Let's say we set the width to ten now and we run this code again.
‫And with the split with once again so we can see that the default value that was given is printed and
‫then the new value that we set.
‫So we set width to ten and now it says the width and it's calculating the width accordingly.
‫So three times, four times ten is one on 20.
‫It's the new volume.
‫All right.
‫Summoning it up.
‫So what we did is we created, first of all, a bunch of member variables.
‫We made them private so that no one except for this class itself can access them directly.
‫So no other class can access them directly.
‫They can only access them via our properties that we created or via our setters and getters.
‫So we have the set length that get length.
‫So if anybody wants to access the length variable, they have to go the way of using set length and
‫get length.
‫This is a totally valid approach and you will see that.
‫And yeah, with those approaches you can define the logic that you want to be followed.
‫If somebody wants to, for example, set a variable or if they want to get a variable.
‫Now another way of doing it is to directly use a property.
‫So if you use a property, you need to call it with a capital letter and then you can implement a getter
‫and etc. but you can also just decide to either only implement a getter or only implement the setter.
‫We're not, of course, implementing both of them here.
‫You can also set the.
‫Values directly, so you can either set the member variables.
‫So this is what we did here by.
‫So to speak, adding a gateway called Height, which is our property to our height that we created here
‫at the top, which is our member variable.
‫Or we just went the direct route where we just said, okay, we will directly have a very simple auto
‫implemented property here and we don't need to have an extra member variable for it.
‫We could have gone the whole way for all of those variables to only have properties.
‫So we could have made it very simple.
‫But just having three properties like.
‫This.
‫So getting rid of all of that and just have a public or apr0p double tap and, and this would be then
‫the length would be just get set and then the same thing for the height and so forth.
‫But I just wanted to show you all of the different ways that there are and all of the different ways
‫that you will see.
‫And most of the times when you see something or when you use properties, you will see them most of
‫the time like this.
‫So with the other implemented property, what is the easiest and fastest way?
‫You will not need to use all of the different approaches that I showed you that is really important.
‫You will not need to use all of the different structures, but in some cases you will need to implement
‫the setter.
‫In some cases you need to implement the specific getter, and now you know how to do it.
‫So in short, if you have any questions, why would you need this approach?
‫Well, they are just used when you have no additional logic required in the property assessors.
‫So in the property definitions and yeah.
‫Getters and setters.