13. How to program in C# - PROPERTIES - Beginner Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
thanks for tuning in at brakus hello and welcome to video number 13 in the how to program in c-sharp course in today's video we're going to have a look at properties also called getters and setters and a property basically allows us to kind of control how we access and change a variable so and that's a very loose definition but I'll show you exactly why this is useful in just a moment first I quickly want to mention that if you have any questions simply go to farm table brackets that come you can write questions there and get answers find the solution to problems in your code and even suggest tutorials if you have any cool ideas so as always let's open up samer in studio and let me give you a good example of a property that is already created for us so when we are using c-sharp and we make sure to import the system namespace that's a class available to us that I haven't shown you yet this is called the date time class and it handles everything you want to do with dates and time on the system so if we want to get the current system time we would maybe create a new variable called a current time and it should of course be of type date time and we would set it equal to date time dot now and you can see that it tells us that this is a public static variable of type date/time and you can see in here that it says get and that's because we are not actually allowed to change this variable if we were able to change this it would say both get colon or semicolon then set semicolon but this only says get and that means that this is a read-only property so basically this is very very good because it allows us to get the current time and then print it out so let me just show you that this gets our current time and deeds is 7:30 and it's the data is right there but we cannot actually change the time on our system it would be ridiculous if every time we made a console application we could just change the current time so it's very good that we have this restriction and as you will come to know a lot of what programming is is restricting yourself and your teammates from doing something stupid I mean when your program becomes more complex everything is about making stuff very clear and also restricting you from doing things you're going to regret Oh which will break the application so let's actually have an a look at creating our own get a property or own getter and setter so let's say that we are making a game and we want to create a class for our player that is going to contain for many an information about our players such as health and man and run speed and all kinds of things for now we are simply going to focus on creating a health variable for this player so we might go in and create a public integer and call this health and set it equal to a default of a hundred now inside of our main we might create a player and we might call him Tom and set him equal to a new player like that and in here we can both print out Tom's Hill and we can also change tom's health so we'd say tom dot health equals 40 and we can print out Tom's health now that we've set equal to 40 so this should print out a hundred because that's what it defaults to set it to 40 and then print out it's the new value which is of course 40 so when we play this you can see 100 and then 40 so that works just as we intended it to but let's say that our player class gets a lot more complicated and we don't want to just change the health no we want to create a method that damages our player because that way we can maybe create some hit particles and a damage animation and the sound when the bullet hits and we can do all kinds of stuff we don't just want to change the health behind the scenes all the time so we maybe want to create a public void called damage and this is going to take in an integer with the damage amount and it's simply going to say that health minus equals the damage amount and this is all fine and dandy we can go in here and we can say Tom that damage and we can maybe damage him by 50 and you should have 50 left so then we can print out we can write out Tom's health after we damage him so you can see it now says 50 and we can have all a bunch of logic in here but a problem would then be if you later continued on to it working on something else or another person on your team went back to this and wanted to damage the player and instead of using Tom damaged they simply went in and said Tom health minus minus equals 50 well this is a problem because now Tom with would lose health and it would play a sound or an animation or update the GUI or his health bar or anything like that his health would simply go down behind the scenes and that would be an issue so a what we need to do is simply restrict this from happening so you might be thinking but let's just go in and make this into a private variable and that's fine we can call Tom damage and damage him by let's say 30 this time but now we're not actually able to write out his health so the solution to this problem is making a get only or a yeah get a an access only or get only property so in order to do this we basically need to create what looks like two variables the first one is going to be a private integer and this is the and I'm going to call this underscore health because it's going to be only accessed from within our own player class and this is going to store the actual health of our player and therefore we can also set this equal to a default value such as 100 then we create a public integer and this is the actual property and this is and this is not going to be underscore this is simply going to be health and instead of setting this equal to something or just closing this off with a semicolon in fact we're not going to be using a semicolon at all instead we open and close two curly brackets and I'm going to put these down on and some separate lines here and inside of this we then write get and again we are going to open and close some curly brackets and then we say return underscore health so this is I get only property so basically what this does is it says that whenever we try to get the current health of our player it's simply going to return the private health amount and this means that we cannot actually change the health variable unless we are inside the same class and are able to say underscore health minus equals damage so down here in our main method we can now damage tom by saying tom damage and we can print out tom dot health but we cannot actually say tom dot health minus equals 20 or equals 20 because this is a read only variable and you can say if I try and do this it says property are indexes indexer cannot be assigned to it is read-only so that's super cool and that makes our code much more safe so now when we hit play you can see it it prints out 70 because we start at a hundred then we call the damage method which subtracts our private health amount by whatever we passed in and what we passed in was 30 so this is now equal to 70 and then down here we access the property here which is this health variable and that simply gives us the value here so it might look kind of crazy to do all of this but it's a very important thing to start doing and this is basically the same as doing this some people like to do this on the same line just say get and in here simply say return on the score health and remember this semicolon there and you can see that that looks a lot smaller and more compressed but maybe not as readable that's completely up to you for now I'm going to do it this the other way because I think it's easier to kind of figure out what's going on so that's super cool but let's say that we want to get rid of this damage damage method and actually just want to change our variable by by using the access you here so at the moment we can of course only get our health we can also make a setter for our health we could just remove this and do instead or and not be able to actually print it out but for now we'll just keep this scatter so down here we are going to say set and on the order in which you do this doesn't matter you can create the setter before the getter or this way around so the seller is pretty similar and what we do here is simply reset underscore health we set this variable up here equal to what every we pass in or we set this equal to so that value that we set it equal to down here when we say that we want to print out time that health and then we want Tom dot health to be equal to 20 this value here up here will be called value so that's the keyword for getting that right there so when we print this out here Tom health it should say 20 so basically this is the exact same as simply using a normal variable when you create the standard getter and the standard setter it's the exactly the same as simply making a public integer and using that but we can do more with this with other than just limiting what whether or not we can get our set values we can also change the way that we get and set a variable and what do I mean by this well at the moment here when we change tom's a health and print it out it simply does so it's it changes 100 to 20 but let's say that we want to kind of limit our health let's say that we only wanted to go between zero and a hundred because 100 might be our maximum health and it would be kind of weird if our player could just be healed infinitely or be damaged so than his health was suddenly negative so if we wanted to limit this health what we can do inside of the setter here is we instead of just blindly setting setting it equal to the value we pass in the say that if he'll or if I'm sorry if a value is smaller than or equal to zero oops then we want to set underscore health equal to 0 else if value is greater than or equal to a hundred well then we want to set underscore health equal to a hundred and else meaning if it's not smaller than or equal to zero or not greater than or equal to a hundred so it's somewhere between 1 and 99 well then we simply want to set our health equal to value so this might look very confusing to you and I will admit that getters and setters comes with some syntax but basically what we can do now is amazing so down here we can say that Tom dot health minus equals 200 so let's say that he falls off a cliff and he takes 200 damage well now we ensured that he will have a health amount of cearĂ¡ and then we might let's say want to heal him by 400 well that will now we have ensured that his health will only go up to the maximum which is which is 100 so even though we write this we've made sure that it only goes from the default hundred down to zero and up to a hundred so that's basically getters and setters for you if we wanted to make this code even more pretty we could go in here and have a a max health a private int called max health and we could simply reference the Mad Max health here and here and that would maybe be even cooler but for now I think we're just going to stick with this so I'm just going to go over the code one last time here just to those of you who think still thinks this is difficult so what we're doing is we are defining this custom class called player and the player is going to have a health variable and we create this first we make it private because we don't want to directly be able to access this variable then we set it equal to a hundred by default however we we want to be able to change the variable just making sure as we go along that is going to be stay between 0 and 100 to do this we create the normal getter that will simply return our health amount whenever we we want it we also create a setter that will check if the value is within this boundary and if not limited to either zero or a hundred and if it is within this boundary boundary it's simply going to set the health value equal to whatever was passed in so if we're down here try and write out the default health it's going to say a hundred then we subtract 200 and it's going to go up here and try and set it and it's going to say that the value here is is actually less than or equal to zero and therefore we want to set it equal to zero and that's all it's going to do and then it's going to write out the zero here and then here it's going to add 400 so it's going to go here again under the setter and say ok so the value is not less than or equal to zero but it is greater than or equal to 100 so it's going to set health equal to 100 and it's then going to write out that hundred then down here if we were to set health equal to fifty and then write that out then it would go up here and it would say okay we want to set the variable it's not less than or equal to zero it's not greater than or equal to 100 so instead we simply set it equal to the value we passed passed in which is currently 50 and then it's going to print this out by using together I'm returning the health so I hope you really cut that and and now know why these getters and setters are useful and don't worry you'll master them in no time at this point we've come very far into writing c-sharp code and you should be able to write most programs out there just using the tools you have now from this point on it's it's just about making your code more reusable and awesome so now let's run this last time and it's going to print out a hundred zero one hundred again and then finally print out 50 cool so I hope you enjoyed this video and I'll see you in the next one
Info
Channel: Brackeys
Views: 171,831
Rating: undefined out of 5
Keywords: unity, unity3d, tutorial, asset, assets, model, texture, models, textures, material, materials, beginner, easy, javascript, java, function, and, settings, how, to, howto, learn, learning, course, series, tips, tricks, tutorials, workflow, fix, tip, technology, game, development, develop, games, programming, coding, basic, basics, C#, property, properties, get, set, getter, setter, getters, setters, access, control, variable, variables, safe, safer, improve, code, efficient
Id: gvQziNULkdY
Channel Id: undefined
Length: 18min 33sec (1113 seconds)
Published: Sun Aug 16 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.