C
C
ChechenProgrammer2019-04-18 23:57:27
C++ / C#
ChechenProgrammer, 2019-04-18 23:57:27

Why/What is this code for?

What is the usefulness? Why is it necessary to return a value? And what is it, to put it simply?

class Empty
    {
        private int age;

        public int GetAge()
        {
            return age;
        }

        public void SetAge(int value)
        {
            if (value > 0)
            {
                age = value;
            }
        }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dollar, 2019-04-19
@dollar

These are getters and setters. That is, property accessor functions.
What for? I'll try to explain in simple terms. It is difficult to download large programs completely into the brain of an individual and represent the entire structure with all the nuances at the same time. Therefore, it is customary to divide the program into parts. Moving the code into a separate function is one of the easiest ways. In OOP, this problem is approached more thoroughly. Class as a phenomenon is an example of this.
The class divides the program into at least 2 parts - what is inside the class and what is outside the class. It is believed that the class is written by one person, and another person uses it. But it can be a single person, it’s still easier, because a person first writes a class, carefully codes and tests everything there, and then completely FORGETS how it works inside, because it is already ready and can be used. That is, a person unloads this information from his unreliable human memory and proceeds to the next task, in which he simply uses this class. All he needs to remember is how to use the class, and all the ins and outs can be forgotten until he needs to improve the class. And here again it turns out conveniently. When we improve the class (it is not necessarily the same person), we can not think about how it is used.
In your example, this is just a blank. Next, you need to fill this blank with functionality. In its simplest form, a getter simply returns a private property, and a setter changes it. So you need to do it right away, so that later you don’t have to rewrite the entire program (and coordinate it with colleagues). But even here there is already a small rule - you can not pass negative values. Conditions may be more difficult. And in general, everything can be more complicated. For example, instead of age, you can store the date of birth. And the GetAge function will each time calculate the difference with the current date. In fact, nothing changes for external use, but magic happens inside.

N
Nikita Yudin, 2019-04-19
@NikitaNike

Now I will try to explain in my own words, and below I will attach more complete and accurate information.
0. age is a private variable that is only available within this class.
1. GetAge() is a public (open to all classes) function that returns the value of a private (hidden from other classes) variable. Often called a getter, from the word Get.
2. SetAge() is a public function that sets the value of a private variable. It is called a setter, from the word Set.
What is it for ?
First of all, for safety as well:
- observance of one of the basic principles of OOP (on which C # is built) - encapsulation .
- so that you can not directly change the variable from other classes.
- in order to set additional conditions.
For example, in SetAge(), you can write a condition under which the age can only be specified in the range from 0 to 100, this will avoid many errors.
Also in C# there is a more convenient syntax: This is if you just need to take or set a value, you can also not write set or get at all if it is not needed. And for more complex logic, you can write it like this:

private int _age;
public int Age
{
    get
    {
        return _age;
    {
    set
    {
        if(value > 0) _age = value;
    }
}

Article on docs.microsoft:
https://docs.microsoft.com/en-us/dotnet/csharp/pro...
Article about encapsulation and what get/set is for:
https://metanit.com/sharp/tutorial /3.4.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question