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

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

Introduction to Object-Oriented Programming in C#

In this video, we will delve deeper into the use of methods, getters, and setters in object-oriented programming. We will also explore the use of the "this" keyword and how to divide long classes for better code organization.

Understanding Getters and Setters

  • Getters and setters are methods used to establish properties for objects.
  • Long classes can be divided to improve code organization.
  • The example used is a car class with a getter method called "getInfoCar".
  • The car class has two unused fields: air conditioner and upholstery.

Setting Properties with Setters

  • The air conditioner field is of boolean type, indicating whether the car has air conditioning or not.
  • The upholstery field specifies the type of material used for the car's upholstery.
  • One approach is to set the values in the constructor, but it may not be suitable if we want different cars to have different properties.
  • A better approach is to create setter methods that allow us to establish individual properties for each car.

Constructing Setter Methods

  • Setter methods are public and do not return any value (void).
  • The name conventionally starts with "set" followed by the property name (e.g., setAirConditioner).
  • Setter methods can be placed anywhere in the class but should follow an order for coherence.
  • After specifying public void, include the name of the setter method.

Conclusion

Object-oriented programming in C# involves using getters and setters to establish properties for objects. By dividing long classes and using setter methods, we can improve code organization and set individual properties for each object.

Setter Method Parameters

In this section, the speaker discusses the parameters used in a setter method. The parameters are passed within parentheses and are used to set values for specific fields in a class.

Setter Method Parameters

  • Setter methods allow us to pass parameters to set values for specific fields in a class.
  • The parameters are enclosed within parentheses and can be of different types.
  • In the example given, a boolean parameter named "airConditioner" is used to store whether the car has air conditioning or not.
  • Another parameter named "upholstery" of type string is used to store information about the upholstery of the car.

Setting Field Values in Setter Method

This section explains how to set field values within a setter method using the passed parameters.

Setting Field Values

  • Within the setter method, we can assign values to class fields based on the passed parameters.
  • In this example, the value of the "airConditioner" field is set equal to the passed boolean parameter "airConditioning".
  • Similarly, the value of the "upholstery" field is set equal to the passed string parameter "upholstery".

Corresponding Getter Methods

This section introduces corresponding getter methods that accompany setter methods. Getter methods allow us to obtain and print information about specific fields in an object.

Corresponding Getter Methods

  • Getter methods are used to obtain information about specific fields in an object.
  • A corresponding getter method should be created for each setter method.
  • In this case, a getter method named "getExtras" is created.
  • The getter method returns a string containing information about certain fields of interest (e.g., upholstery and air conditioner).

Implementing Getter Method

This section explains how to implement a getter method and return the desired information.

Implementing Getter Method

  • The getter method should have a public access modifier to allow access from outside the class.
  • The return type of the getter method should be specified, in this case, it is a string.
  • Inside the getter method, concatenate the desired information using appropriate syntax (e.g., field name and its corresponding value).
  • Use the "return" keyword followed by the concatenated string to return the information.

Obtaining Extras of a Car

This section demonstrates how to obtain and print the extras of a car using the implemented getter method.

Obtaining Extras

  • To obtain the extras of a car, call the "getExtras" method on an object.
  • In this example, we call "getExtras" on car 3.
  • By executing the program, we can see that it prints out general information about car 3 as well as its extras (if set).
  • If no values are set for certain fields (e.g., upholstery or air conditioner), default values will be displayed.

The transcript provided does not include timestamps for all sections.

Default Values of Variables

The default value of a Boolean variable is always false, while the default value of a string variable is an empty string.

Default Value for Boolean Variables

  • The default value for a variable of Boolean type is always false.
  • This means that if we don't assign any value to a Boolean variable, it will be false by default.

Default Value for String Variables

  • The default value for a variable of string type is an empty string.
  • An empty string means that there are no characters inside the quotes.
  • If we don't assign any value to a string variable, it will be an empty string by default.

Setting Extras for an Air Conditioner

To set extras for an air conditioner in the main class, we need to use the setter method before calling the "goethe" method. We can establish what extras the air conditioner has by using the instance name and calling the setExtras method with appropriate parameters.

Setting Extras for Air Conditioner

  • Before calling the "goethe" method, we should establish what extras the air conditioner has.
  • To do this, we use the instance name (in this case car3) and call the setExtras method.
  • In this method call, we pass two parameters: one of type boolean (for air conditioner) and another of type string (for upholstery).
  • The visual studio detects that these parameters must be passed in this specific order because that's how we built the setExtras method.

Passing Parameters to Set Extras Method

When passing parameters to the setExtras method, make sure to pass two parameters in specific types - boolean and string. This ensures that there are no errors in the method call.

Passing Parameters to Set Extras Method

  • In the setExtras method, we refer to the class fields for air conditioner and upholstery.
  • We also refer to the parameters received by the method: one for climate control (boolean) and another for upholstery (string).
  • It's important to pass these parameters in the correct order and types specified by the method signature.
  • If there is an error in passing parameters, it will be indicated at this point.

Setting Extras Example

An example of setting extras for a car is demonstrated. The car3 instance is given air conditioner as true and leather upholstery as an additional string parameter.

Example of Setting Extras

  • After calling the setter method, we can specify the extras for car3.
  • In this example, we set air conditioner to true and pass a string parameter (in quotation marks) indicating that the upholstery is leather.
  • The main method executes top-down, so it first gives general information about car3, then sets a series of extras including heated seats, leather upholstery, etc.

Default Upholstery for Cars

By default, cars have fabric upholstery. However, it is common to change it to a higher-quality material like leather. This means that fabric upholstery can be considered an extra feature.

Default Upholstery

  • All cars have a default upholstery which is usually fabric.
  • It's common for cars to come with fabric upholstery from dealerships.
  • Changing the default fabric upholstery to something like leather would be considered an extra feature.

Customizing Car Upholstery

While our program does not have fabric upholstery as a default option, we can establish it as an extra by specifying it in the builder. This allows us to customize the upholstery material for our cars.

Customizing Upholstery

  • If we want our default car to come with fabric upholstery, we can add a line in each builder where the upholstery is set to "fabric".
  • By adding this line, we establish that the initial state of our cars includes fabric upholstery.
  • This way, when calling the getExtras method without giving any extras, it will return that the car has fabric upholstery and false for air conditioner.

Constructor's Utility in Setting Initial State

Constructors are useful for setting the initial state of an object. In this case, constructors are used to specify whether a car has fabric or leather upholstery as well as other extras.

Constructor's Utility

  • Constructors allow us to give an initial status to an object.
  • In this case, constructors are used to specify whether a car has fabric or leather upholstery and other extras.
  • By using constructors, we can easily set the initial state of objects and define their characteristics.

Changing Extras for Car3

By removing the commented line that sets extras for car3 and executing the program again, we can see how the default values (leather upholstery and true air conditioner) are applied.

Changing Extras

  • By removing the commented line that sets extras for car3 (setExtras method), we revert back to using default values.
  • When executing the program again without giving any extras explicitly, it shows that car3 has leather upholstery and true air conditioner by default.

Referring Class Fields and Method Parameters

When establishing parameters in setter methods, it's important to use different names than the class fields to avoid confusion.

Referring Class Fields and Method Parameters

  • When establishing parameters in setter methods, it's important to use different names than the class fields.
  • This helps avoid confusion between the two and ensures that the correct values are assigned to the corresponding variables.

Ambiguity in Parameter Names

In this section, the speaker discusses the problem of ambiguity that arises when parameter names are the same as class fields. The interpreter cannot determine whether the reference is to the parameter or the class field.

Ambiguity in Assigning Values

  • When a parameter has the same name as a class field, it creates ambiguity in assigning values.
  • The interpreter does not know if we are referring to the parameter or the class field.
  • This can lead to incorrect assignments and unexpected behavior.

Difficulty in Choosing Different Names

  • It may be challenging to come up with different or better names for parameters and class fields.
  • In complex programs, it is common to encounter situations where a method's parameter has the same name as a class field.

Green Underline Indicating Ambiguity

  • When there is ambiguity in names, Visual Studio underlines instructions related to assignment with a green underline.
  • The green underline indicates an ambiguity warning rather than an error.
  • Hovering over the green underline shows a message stating "assignment to the same variable."

Incorrect Behavior Due to Ambiguity

  • If ambiguity is not resolved, it can lead to incorrect behavior in program execution.
  • Example: Even though leather upholstery and air conditioner values are assigned correctly, they are giving fabric and false respectively due to ambiguity.

Resolving Ambiguity with "this" Keyword

  • To differentiate between class fields and parameters, use the reserved word "this" before referring to a class field.
  • Adding "this" clarifies that we are referencing the class field rather than the parameter.

Correcting Behavior with "this" Keyword

  • By using "this" keyword appropriately, we can resolve ambiguity and ensure correct behavior of setter methods.
  • After resolving ambiguity, executing the program again will show expected results for upholstery and air conditioner values.

Dividing Classes for Manageability

This section discusses the utility of dividing classes into smaller pieces to manage complex programs more effectively.

Managing Complex Classes

  • In real applications, classes can become lengthy and complex, making it challenging to locate errors.
  • Dividing a class into smaller pieces can help in managing and organizing code.

Benefits of Dividing Classes

  • Dividing classes makes it easier to locate errors and manage complex codebases.
  • It allows for better organization and improves readability of the code.

Using Partial Classes

  • To divide a class, use the reserved word "partial" before the class definition.
  • Define different parts of the class in separate partial class blocks.

Example Division of a Car Class

  • For example, a car class can be divided into two parts: one containing constructors and another containing getters, setters, and fields.
  • The division can be customized based on specific requirements.

Syntax for Partial Class Division

partial class Car

// Constructors

partial class Car

// Getters, setters, and fields

Benefits in Real Applications

  • In real applications with large codebases, dividing classes into manageable pieces helps improve maintainability and ease of debugging.

New Section

This section discusses the visual division of code and the use of the solution explorer panel in Visual Studio.

Visual Division and Commenting

  • It is possible to visually divide code into separate sections for better manageability.
  • Comments can be used to indicate a visual division within the code.
  • This feature is not currently needed, but it can be useful for organizing and understanding complex code structures.

Partial Classes and Code Recognition

  • By using the keyword "partial" in both classes, the builder recognizes class fields declared in other parts of the class as if they were part of the same class.
  • This allows for easier management of long classes with extensive code.

Solution Explorer Panel

  • The Solution Explorer panel provides a helpful overview of files and items within a project.
  • When expanding a file, such as the program or car item, all methods, class fields, properties, and their types are displayed.
  • This feature is particularly useful when working with large classes containing thousands of lines of code.
  • Selecting a method or class field in the Solution Explorer takes you directly to its declaration in Visual Studio.

Navigating Code with Solution Explorer

  • The Solution Explorer helps navigate through complex code structures by providing quick access to specific elements.
  • If you are unable to locate where a property or method is defined, simply click on it in the Solution Explorer to be directed to its declaration.
Video description

Vemos en este vídeo el uso de métodos setters, getters, la palabra reservada "this" y la división de las clases en trozos o partes para hacer más fácil su manejo. Para más cursos, ejercicios y manuales visita: www.pildorasinformaticas.es