Curso C#. POO V. Getters y Setters. Vídeo 31

Curso C#. POO V. Getters y Setters. Vídeo 31


hello that such welcome everyone to another delivery of this C # course in which we are going to continue advancing in the object-oriented programming and in this video we are going to see several things let’s continue talking in depth a little more in the use of methods routers and setters I already mentioned from previous videos, what are they for? what are these types of methods we will delve into them a little more let’s talk about the use of a keyword as a many languages object oriented that is the word this we are going to see one of the utilities what can be given to this word reserved itself sharp and let’s see also how to divide itself sar the code of the long classes many times to the time to develop complex applications we realize that our classes they are excessively long in terms of code to make your handling in sisal and in visual study there is the possibility of dividing those classes let’s go to it to see all this let’s continue working with the example of the video previous if you remember we had created a class I use cars with its main method and then we have created our class car which had an overload of builders we have already explained this to what are you referring to and taking a look at this car class we did in videos previous we realize that there a getter method that is get info car that gives us information about each one of the cars that we can build the wheels that has the length and the width and we realize that there are two fields of class that we have not yet used in our program that are the field air conditioner and the field upholstery are refer to extras that may have our cars when it comes to building a good car because we can establish that has climate control or that does not have it that is why this field is of the buljan type because if you have air conditioner we are going to make it true and if it does not have air conditioner that is false the field upholstery that this spring type well to specify if the Upholstery is made of fabric is leather or any other material but not yet we have then used the question that it arises is how can we establish for each of our cars if one has air conditioning if the another does not have what kind of upholstery has every car and so on there are several alternatives we can establish the value of these fields air conditioner and upholstery in the constructor or in the first builder or else in the second we can do perfectly the following we can tell you that the air conditioner in the first builder for example let’s see if we write it well is equal to true it was julian type but of course this what it gets is that all the cars that let’s build using the first default constructor is to say as initial state have air conditioner maybe you want it to be like this if this is your I want to build your program perfect but what I want is to be able to establish each car if it has or it does not have air conditioner in that case it is not good idea to say conditioner true in a constructor since I would give it by default If you have air conditioning so what is most appropriate in this case would be to build what is known as a setter method a setter method is that that establishes from there what to be set in English means to establish them Name these methods because are dedicated to establishing properties to the objects not then what’s right would be to build a setter method that is able to establish if our objects they have or do not have so much air conditioning as the kind of upholstery that they have how are these types of methods constructed it’s very simple, let’s situate ourselves by example following the method previous car type although what we could do anywhere in the good class then to follow an order and a coherence I will do it what I will build after the previous method and before the fields of class and it would be like this in first place the access modifier public so that this method set the one let’s build be accessible from out of this class I need to be can access from outside for that public this setter method is going to return nothing is one of the characteristics that have the set in the eternal do not have instruction reiterate your goal is not give us back information is what otherwise the objective of a setter is establish information give value to properties for that reason Following the public, we can not specify neither string nor int or double or anything like that because it’s not going to return anything the methods that do not they return nothing in themselves sharp and in others languages are methods of type void if os you set the main main method of all our classes is a method of type void because the main never returns anything you will never find an instruction return within a main method It is very easy to distinguish when I should use the void and when not if you method carries return does not carry void and if it takes I’m going because there’s no return is that simple and after the void the name of the method and here I tell you something similar or equal to what I said to the time to build the method that info car if you remember the previous video os I said that when building the method that you car info the name of the method may be what you you want to give me car information give me info car as you want but for convention since it is an ether is usually use as the name of the method the reserved word that and then what you want the same thing happens with a setter method you can call it as you want but by convention the name of these methods is the reserved word set and then the name that want like this method what is going to do is going to be establishing the extras of our cars could call set extras we open the parentheses of the method and within these parentheses those parameters that we want to pass on to method if we need it and in this case yes we are going to need to spend parameters to this setter method for start I’m going to pass a parameter of bull type and which I’ll call by example for me air conditioner in this parameter I intend to store then if the car that we are building in each has or does not have air conditioning and as a second parameter it has to be of the string type since the upholstery a field of type string because I’m going to call for Upholstery open the key to the method setter we are closing it inside this method let’s do something very simple we are going to tell you that the class field air conditioner will be equal to what you let’s go to this method by parameter they put on air conditioning in such a way that if when we use this setter method with an object here we passed tricks s true it is stored in air conditioner if here in paraná heated we pass false because that falls will be stored in climate control and we must do the same with the other field and the other parameter let’s tell you that the upholstery field of the class has to be equal to parameter upholstery that will receive this method when you were invoked when you I called and it is already with this we have built our first setter method normally a setter method is accompanied by its correspondent jeter is to say if we have a method to set the extras his would be have also a method to obtain those extras to print in this case in console what are the extras of each vehicle we have the possibility of obtaining the extras from the same method get info car that we build in the video previous but I’ll do it in this separate case is to say I’m going to create our first method pair jeter and setter that is to say method extra set and its corresponding goethe jeter that would be very similar to the one we did in the previous video for get car information first place modifier access pavlik for that can be accessed from outside and Now we have to specify this method getter what kind of data do we will return and what will return us it’s going to be a string exactly like as we did in the car info get like this that pavlik string and now the name that we are going to give this method good because in this case it will be called get extras not going to receive any type of parameter this method and it gives us error until not we specify with the instruction return that will return us in this case the visual studio marks us error because detects that it is not void is string and as it is string must return obligatorily a data or information so up that we do not complete the return instruction That error will not disappear It’s going to be very simple, as I was saying be a method that will return us return a string look like now the error of the name of the method and here we are going to put by example the following car extras for example two points a bar inverted and n to make us a jump of line and here we start to concatenate for example tapestry two points a space and this concatenated with the field upholstery of the class with the value what is stored in that field and to then what we have here with air conditioner left a small space between the quotes and air conditioner for that you do not write everything down when give us back the information two points quotes and this concatenated with the Class air conditioner field and here point and eat now this is what is going to return that method just as we have the program then we can perfectly obtain the extras of any of our cars Of course, if everything goes well, tell us that upholstery there is none in absolute and climate should tell us What are we going to see if it’s true we go to our main class where we have built from the previous video car 1 2 and 3 remember that here already we had asked for the general information of car 3 well then let’s ask also with another one with are rail line to return us to tell us what extras has that car three so car 3 fixed point you like in the menu visual studio intelligence appears between the list of methods get info car which is the one we use in the previous class but we also see here the method that extras is the one that we just built well since get extras if we save changes and execute the program with control f 5 well here the console gives us all the information that we have specified in the main start car information 1 is the information of car 2 and is here where the information starts car 3 gives us the general information and that’s what we got with the call get info car tells us that it has four wheels the length and width in concrete and then go into action get extras and that’s what gives us back then it does not tell us extras of car here put a semicolon without I should have put two points white upholstery as I told you and climate control false because he returns this to us because he we are saying that we of the extras of car 3 but we have not used the setter we have not established if this car has upholstery or what kind of Upholstery has and if it has air conditioning or not let’s analyze why the upholstery appears blank and the air conditioner appears on false if we have not established if it has it does not have air conditioner because it gives me false If we have not established the type of upholstery because it comes out in good white because this is due to the default value of the variables of the fields we for now, nowhere in our code we have told our cars what is the value of the truck or air conditioner? fons since this guy bull and neither did we have said what value has upholstery which is of type string when you in sisal not specific what value does a variable this takes the default value the default value of a variable of Boolean type is always false for that air conditioner tells us false and the value default of a type variable string is always an empty string is as if inside the quotes there we will write nothing why it goes blank because a string is a string of text empty if we want to establish air conditioner and upholstery and then get that information we should go to the main class and use the method setter before the goethe that is before to ask for the extras we must establish what extras he has and that we could do just before calling precisely to the goethe extra method if we do it with the instance name in this case car 3 which is what we want set the point extra and it’s now where we use the set extras method notice that when you open the visual parenthesis studio detects that this method must receive in the call two parameters one of type buljan and another of type string because that is how we built it and that is what we must pass to him until he we pass two parameters and that type in particular buljan string the error that is marking us will appear right now we’re going to tell you that this car 3 if you have air conditioner true and let’s tell him by passing him a string in quotation marks that the upholstery by example is going to be leather after the call to the semicolon setter method and you know that the main method is executed top down, well, first of all will give me the general information of car 3 then going to give car 3 a series of extras that is heated on trump leather upholstery and then I will return what are the extras of that car if you save changes and executed the program with f5 look like now that is telling me that the upholstery is leather and conditioner true we could have used for the air conditioning a variable of type string also so that it does not come out true but put the air conditioner yes or climate control no or anything else perfectly the only thing the only reason why I did type william is to start practice with this type of variables with julian type variables but what you could have also declared as string as we have done with upholstery arrived at this point and to continue with the logic when it comes to building a kind of car type all cars they have a tapestry then it’s that pay more not in the dealership of cars and the upholstery that the default cars as you change it by a top upholstery that is to say is very frequent that cars come with a cloth upholstery and that upholstery of fabric, if you want to pay a little the more you change it for a leather one then the leather upholstery would be an extra we do not have our program like this our program does not work from that shape our cars by default not they have a fabric upholstery yes that we allows to establish as an extra leather upholstery as we have seen now and if we feel like any other material but if we want that our cars have a default cloth upholstery we should do surely many of you already you know and if it is happening to you if I I want my default car to come with a simple fabric upholstery what What should I do? that cloth upholstery inside the builder that is to say as state initial of my cars that the upholstery it’s made of fabric, I just have to add in each of the builders that the Upholstery will be equal to fabric both in the first constructor we did in the previous video as in the second builder in such a way that if I now I keep changes commented or I remove the line where I’m giving extras to car 3 and without giving you extras because this line now same is commented I ask for the extras with the method that you extra should tell me that has fabric upholstery and that the air conditioner is false if I keep changes and executed we see how it’s really like that here you have how is giving by default a tapestry of maybe this is what I just did you finish understanding everything we saw in the previous video the utility from a constructor give initial status to an object situ this initial state in the what the upholstery is made of fabric what do you want change for a leather upholstery give him those extras now I remove the line commented on set extras and now by giving it what it does is replace the leather upholstery for the upholstery of fabric in such a way that if I keep and executed we see as now the upholstery it is leather and air conditioning true if we take a look at this first setter method that we have built where we are setting up the extras here we have it pavlik I go set extras will blame string conditioner for upholstery look at one thing within this method setter I’m referring by a side to the field of the class that is called air conditioner because we have declared it within the class here is and also I’m referring to the field of upholstery class because we have declared in the class here is upholstery and on the other hand I’m referring to the parameters that receives the method on the one hand for the climate control and on the other hand for in upholstery but imagine the following imagine that when it comes to establishing parameters in the setter method then in instead of giving a different name to the one has the class field seems to me I happen to give the same name is to say instead of calling the first parameter for an air conditioner I call it climate control and instead of calling the second parameter for upholstery as I call and it would be exactly the same as the name I’ve given the class fields that happens in this situation good too I have to change the name obviously within the method no longer exists for the air conditioner because I have replaced by air conditioner no longer exist for upholstery because I have changed for good upholstery well here there is what is called a problem of ambiguity since the parameter is called exactly like the class both in air conditioner like upholstery when I I intend to assign the value with the instructions inside the method the interpreter of sisa does not know exactly if I’m referring when I put air conditioner to the parameter or to the class field and the same goes for upholstery you do not know if I mean the parameter or to the class field because they have exactly the same name this complexity takes it from between doing what I have done previously calling the parameters differently from class fields but there are many times that it is difficult for come up with a different or better name said it is tried that the name of the parameters are the most identifying possible to see many times in complex programs in any object-oriented language that happens this situation that the parameter of a method has exactly the same name that a class field fixes that visual study underlines me in green these instructions the underline in green no indicates error but it does indicate another circumstance indicates perhaps a warning if we leave the mouse still above that underline in green fixes who is telling me assignment to the same variable I wanted to assign another element is to say there is not an error like but there is an ambiguity in the names if we leave this like this we keep changes remember that to the car 3 I have it up here we gave it and leather if we leave this situation like this and we keep changes and we execute we see as even though I’ve given leather upholstery is giving fabric and even though the age or True conditioner is giving you false is say our setter method is not working as it should and it is for this ambiguity because he does not know what we we refer to how we solve this situation if we do not want or can not for other circumstances change the name of the parameters well then there comes into play the reserved word dis we will see throughout the course that you can give different uses to this reserved word in programming object-oriented and one of those uses is to differentiate when I refer to a class field and when I refer to a parameter when you want to do reference to a class field you should use the word this in front of that class field is to say yes now I put this point climatizador what I am doing with this instruction is to tell to the interpreter of if sharp to our program if you know that that air conditioner makes variable makes forgiveness reference to the class variable to the class field the same thing would happen if we put this here point tapestry with this I tell you that it is air conditioner refers to dis that is to say to this object in which we we found the class itself in the field of class and the same goes for upholstery surely you are thinking that what complication why not call it differently as we have done a instantly and you can dispense with the use of dis good we will discover later in the course when the programs are more complexes that this is not always so recommended and there the game would come into play use of the disc if we keep changes and we execute the program again we see how now it works giving the leather and the air conditioner that we have established in the call to setter method and to finish this video, I’m going to mention a utility that has the shisha language and visual study that is the possibility of dividing our classes in pieces look at these applications that we are making us who are extremely simple because the only objective that have is to teach you the basics of object-oriented programming but when you go to real is to say when you go to schedule a real application and you use programming oriented to objects you use classes methods fields of class and so on the classes are extremely long this one is not true that it has a series of lines of code as you can see our class car but neither is it too long it is not uncommon that a class complex of a real application then can have at best 2,500 or 3,000 lines of code in those cases manage to when programming by the programmer managing those classes is quite expensive to locate an error Sometimes it costs good because there is a small help that consists in dividing a class in pieces we will do it with the ours that is not necessary because it is very small but for you to see how it when you want to divide a class in pieces the only thing that you should to do is first determine what do you want me to be in every piece that is to say we have a car class imagine that let’s divide that car class into two pieces, so you have to determine what you want it to appear on the first piece and what do you want to appear in the second piece our class as it is built maybe it would be good idea that the first piece so I have the overload of constructors and the second piece well have the gaiters the sets and the fields of class for example although this You can change the division what you want in that case what you should to do is to use the reserved word partial before the definition of class class car class and open the key as we have done normally and close the key of this partial class wherever you want in our case it’s at end of the builders there is now we get a lot of code error because we lack something and it is to establish the second stub the second stub will begin with just before the first good getter then there declare again partial class car you open the key and that key that opens is not necessary close it because we already have it would correspond to the closing key of the class that was already look like now all the errors that we have now the car class divided into two pieces this makes it more manageable here we can even include a comment in this way that indicates a good visual division since you know that this is possible, we are not going to need us for the moment but it’s possible to do it in the shisha and in visual study if we keep changes and we execute the program we see that this one it continues to work exactly the same we see that by using the word partial reserved in both classes in the builder are recognizing the class fields that we have declared in the other part to the class is ultimately as if it were the same class and also tell you that when classes They are very long and have a lot of code it also helps us a lot the solution explorer panel in the explorer of solutions we must of have a file that is the program if we display this file we see that there are two program and car items and if we deploy a car, which is what it does reference to our class we see that here he points us in this explorer of solutions all the methods and all the class fields all the properties and besides their types the help consists of that when you have a class with thousands of lines of code if you select a method or a class field in the visual studio solutions explorer takes you to for example if you do not find where the wide property is defined then you click on the property wide in the solutions and visual explorer study takes you exactly where that wide property is declared if not you find for example the set method extras, you click on set extras and visual study takes you automatically where it’s declared that method set extras is to say this explorer also helps when classes are enormously long well let’s leave this video in the point where we are and you I say the usual I invite you to Stay tuned for the next installment until then you do well