Getters & Setters | C# | Tutorial 28

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome to draft Academy my name is Mike in this tutorial I'm gonna talk to you guys about using getters and setters in c-sharp now a getter and a setter are basically two types of methods that we can define inside of our c-sharp classes which will essentially control the access that people have to the attributes of those classes and essentially what this does is it kind of like makes your classes more secure so it kind of allows you to define like what data is valid for specific attributes and what data isn't valid and really gutters and setters are extremely useful there's a lot of different scenarios where you want to use them in this tutorial I'm gonna show you guys one specific scenario we're gonna talk about like how to set these up and essentially just how they work so what I've done in preparation for this tutorial is I actually created in another class it's this movie class over here and this movie class is very simple it's basically just modeling a movie and inside of our program so this is sort of like you know the template for the movie data type and I'd said that every movie is gonna have a title a director and a rating and then down here I created this constructor where I basically pass in a title a director and a rating and I assigned all of those values so this is a very simple class and you know like I said it's just modeling a basic movie and then over here in my program I created two movies so I have one called Avengers and it's just the Avengers with director Joss Whedon and it's pg-13 and then we have Shrek with director Adam Adamson and it's rated PG so pretty simple program right we're basically just creating a couple different movies but here's one of the problems that we actually have with this program and it might not be super obvious right away but the problem has to do with the movie rating right now generally when movies have ratings they can only have specific ratings right so if we have a movie like generally it's only gonna be able to be G PG pg-13 R or NR right and maybe there are some more ratings whatever but let's just say that these are all the valid ratings that we can have for a movie so a movie can either be rated G PG pg-13 R or NR we're not rated and let's say that we want to enforce these rules inside of this movie class so we want all the movies and our programs to only be able to have one of these ratings and it can't have a rating other than the ones that I specified down here right well the problem is how do we enforce something like that right because what I could do is I could easily come over here and change the rating of Avengers to dog for example and now if I was to come down here and print out the Avengers rating for example if I came down here said console.writeline Avengers dot rating when I print this out it's actually going to print out the rating so it's gonna be able to print out dog so dog was the rating that we gave to it but dog isn't one of the valid ratings down here another thing I could do is even if I set this to pg-13 over here so if this was like set to pg-13 I could still come over here and manually set it so I could still say like Avengers dog rating is equal to you know dog over here and it's gonna be the same scenario right I'm still able to set this value equal to dog so what I want to show you guys is how we can prevent this from happening in other words how can we make it so that the only possible ratings for the movies are one of these five ratings that I defined down here we can actually use something called getters and setters and in order to use these getters and setters we're gonna have to modify a couple things so the first thing that we want to do is we want to close off access to this rating property this rating attribute and what I can do is I can actually come over here into my program and you'll see that all of these attributes are public so we have public title public director and public rating and I haven't really talked about this too much yet but public essentially means that these can be accessed by anyone so any other program any other code that we're dealing with can access any of these attributes so public just means it's open to everybody it's open to the world but there's actually another modifier that we can put in front of here and instead of saying public I could say private and what this means when I say private this means that only code inside of this movie class is going to be able to access the rating so only code that is contained inside of these curly brackets over here in the side of this movie class is gonna be able to access this and you'll see now when I go over to program I'm getting an error down here so essentially what this is saying is we can't access the rating because it's private so because the code over here is inside of the program class and it's not inside of the movie class it's not going to be able to access the rating so that's the first step in being able to enforce these ratings is we need to make the rating field private so I need to cut off access from anybody else now the next thing I want to do is I want to create two things called getters and setters so we're gonna create getters and setters for this rating attribute and basically what these are gonna allow us to do is it's gonna allow outside code for example the code inside of this program class like down here it's gonna allow code like this to both access the rating and modify and set the rating so those getters and setters are gonna be really important and we can define those through what are called properties and a property is basically just like it's kind of like a little method but it's a special method that's going to basically allow us to define a getter and setter so what I want to do is I can just save public and I can just say string and what I want to do is name this generally what you're gonna do is you're gonna name it the same as the attribute over here but in capital letters so I'll name this public string rating and then down here I'm gonna make an open and close curly bracket and inside of here I need to define two things or I don't need to but I'm going to define two things so I can just say get and want to make an open and close curly bracket and then I'm gonna say set and I'm gonna make an open and close curly bracket and these are essentially going to be getters and setters so a getter is going to allow us to get the rating attribute you'll notice over here I'm trying to get this rating attribute by Avengers dot rating but I'm not able to do it right I can't actually access this rating because it's private and so the job of the getter is to give me that so I'm just gonna come over here and inside this get we're just gonna say return rating and I'm gonna put a semicolon there and so basically whenever we say rating now and we want to get it it's going to return the rating now this setter is going to allow us to set the rating so it's gonna allow me to modify the rating so if I came over here and said Avengers dot rating and you can already see it's trying to make me use this capital letter is equal to dog it's gonna yell at me because this is a private attribute so it's not gonna let me do that and that's what the that's the point of this setter the setter is basically going to allow me to do that so what I can do in this Center though is I can define a specific rule so I can basically say like hey you can only set this value to be a certain value and so what I want to do is I want to check to see if the value is one of the valid ratings and if it is if the value they're trying to set rating to is one of the valid ratings then we're gonna go ahead and set it otherwise we're just gonna do something else so I want to say if and I want to check to see if the value that they passed in in other words the value that they're trying to set to rating is equal to one of the valid ratings so what we can actually do is we can say if the value and value is gonna represent whatever got passed in so value is gonna represent whatever they're trying to set the rating to so I can say if value is equal to and we're just gonna do one for all of these so if the value is equal to G or value is equal to PG and I'm gonna do this one for each of the valid ratings so I can just say or value is equal to and over here we can just put pg-13 so I'm gonna say pg-13 and we're just going to do two more so value is equal to R and then the last one is going to be for NR so and over here I'll say or value is equal to NR so if it's equal to one of these in other words if the value that we passed in equal to one of these then we're gonna be able to just set that as the rating so I can just say rating is equal to value otherwise though I'm just gonna set the rating equal to NR so I'm just gonna say rating is equal to NR so if they add in an invalid rating in other words if they add in a rating if they try to set this to a rating that's not G PG pg-13 R or NR then it's just gonna get set to not rated by default so this getter is going to allow us to get the rating and this Center is going to allow us to set the rating but you'll notice that in order to set the rating we have to go through this if statement right so they can try to pass in whatever rating they want but if it's not equal to G PG pg-13 etc then it's just going to get set equal to not rating and the reason we needed these getters and setters was because we made this guy up here private so there's one more thing we have to do in order to make this super secure in other words there's one more thing that we have to do to make sure that the rating is only set to one of these values up here in my constructor when I set the rating right here I'm just assigning it a normal value so technically the user can pass in whatever rating they wanted over here and then I'm just gonna set it to the rating but now instead I'm just gonna say rating is equal to a rating and what this means is now we're gonna set the rating through this setter down here when I use this capital R it's essentially calling this sutter down here and setting it that way so now this is completely secure and down here what I can do now is I can set this to whatever I want and it's gonna be safe so down here I can say Avengers dot rating so I'm using this capital R now and over here if I wanted I could pass in an invalid rating so I could pass in like dog over here and when I print it out you'll see that we're gonna get NR so we're not going to get dog we're actually gonna get n on and that's because we had to go through that said ergh and the same goes for anything else so I could say down here like Shrek dot rating is equal to cat and if we tried to print out Shrek dot rating then we're gonna get NR because it's gonna get filtered out by that setter but here's the thing if I wanted to set this to a valid rating like if I set Shrek dot rating equal to R now it's gonna be able to take that rating because it's valid so that's kind of how we can use getters and setters and a lot of times what people will do is they'll create getters and setters for each of the attributes inside of their classes you don't have to but a lot of times they can be useful so what you want to do is just play around with that and you know basically these just make your class more secure so they make it so someone couldn't for example give your movie in invalid rating hey thanks for watching if you enjoyed the video please leave a like and subscribe to drop acad to be the first to know when we release new content also we're always looking to improve so if you have any constructive criticism or questions or anything leave a comment below finally if you're enjoying dropper Academy and you want to help us grow head over to Draft Kadim EECOM forward slash contribute and invest in our future
Info
Channel: Mike Dane
Views: 113,295
Rating: undefined out of 5
Keywords: Programming
Id: EbW-1fPhXQE
Channel Id: undefined
Length: 12min 9sec (729 seconds)
Published: Wed Nov 08 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.