Question How can a get function return something it never received?

BrunoB

Well-known member
Joined
Nov 19, 2022
Messages
96
Programming Experience
Beginner
Hello, how can this be possible? It is as if an integer type variable holds a 5, for example, but 5 was never assigned to it.
PS : edad attribute is private
Thanks
C#:
Public void setEdad(int e)
{ edad=e; }
Public int getEdad()
{ return edad;]
 
Last edited:
The compiler will first check if edad is in the most local. If it not there, it will see if it is in the higher scope.
C#:
class Persona
{
    private int edad;

    public Persona(int e)
    {
        this.edad = e;
    }

    public int GetEdad()
    {
        return edad;
    }
}

So it this case, it will first check within the GetEdad() scope. If not there, it will then check if it is in the Persona class scope. Using the keyword this (on line 7), tells the compiler to use the class scope explicitly.
 
@Skydiver this too might enhance the explanation:

C#:
class Persona
{
    private int edad;

    public Persona(int edad)
    {
        this.edad = edad;
    }

    public int GetEdad()
    {
        return edad;
    }
}

This is which each relates to:

View attachment 2645
 
Last edited:
Try to avoid this code pattern if you can; naming local variables the same as class level variables is a recipe for confusion and leads to bugs. Typically we name class level fields with a leading underscore to reduce chance of creating bugs:

C#:
class Persona
{
    private int _edad;

    public Persona(int edad)
    {
        _edad = edad;
    }

    public int GetEdad()
    {
        return _edad;
    }
}

In this way it's really obvious which is which, and you don't need to remember that "this" is used to refer to the class level variable. When you see a variable named _likeThis you automatically know it is a class level one, not a local one
 
@Skydiver this too might enhance the explanation:

C#:
class Persona
{
    private int edad;

    public Persona(int edad)
    {
        this.edad = edad;
    }

    public int GetEdad()
    {
        return edad;
    }
}

This is which each relates to:

View attachment 2645
Sorry , the link you put appears "Oops! We ran into some problems.
The requested page could not be found. "
Ok thanks for the information about the _ for the videos that I saw, they say that the private attributes are named in lower case and the properties or function with initial capital letter
 
The compiler will first check if edad is in the most local. If it not there, it will see if it is in the higher scope.
C#:
class Persona
{
    private int edad;

    public Persona(int e)
    {
        this.edad = e;
    }

    public int GetEdad()
    {
        return edad;
    }
}

So it this case, it will first check within the GetEdad() scope. If not there, it will then check if it is in the Persona class scope. Using the keyword this (on line 7), tells the compiler to use the class scope explicitly.
As I understand it, the private variable age is global and supposedly set and get of the functions are local, is this correct or incorrect?
 
This is the missing attachment; not sure what happened to it

98E8684D-F504-4C51-98F4-8345C0C91406.jpeg
 
As I understand it, the private variable age is global and supposedly set and get of the functions are local, is this correct or incorrect?
Not "global" in the sense of "throughout the entire program", but it's available all through this class, sure

I dont really know what you mean by "local" - "local function" has a specific meaning in C# and nothing in this thread is a local function

Whatever video you're following about methods called "getXxx" and "setXxx" I recommend to stop watching it. It's like a Java person is trying to teach you C#, and it's mostly wrong for what we expect in C#

Use properties for get/set behavior instead, and name methods LikeThisWithAnInitialCaps

Really your class should be more like:

C#:
public class Persona
{
    public int Edad {get;set;}

    public Persona(int edad)
    {
        Edad = edad;
    }
}
 
Ok thanks for the information about the _ for the videos that I saw, they say that the private attributes are named in lower case and the properties or function with initial capital letter
Sure, and some people do code that way, but then it causes confusion and bugs if you forget to put `this` or put it in the wrong place, or forget what it means..

We tend to care less about private variable naming because we don;t see them when we use other people;s code.. But if i was taking over maintaining your code, I would for sure refactor them all to start with _

You can get VS to help you with this:
1674070157957.png


Naming violations are flagged up:



1674070266311.png


And in the lightbulb menu for the green wiggly line:


1674070316401.png



With a few clicks I can have VS fix all the name problems everywhere

---

For more complex things like Async methods not having Async at the end, you can either using naming styles, or use an extension like AsyncNameFixer
 
Not "global" in the sense of "throughout the entire program", but it's available all through this class, sure

I dont really know what you mean by "local" - "local function" has a specific meaning in C# and nothing in this thread is a local function

Whatever video you're following about methods called "getXxx" and "setXxx" I recommend to stop watching it. It's like a Java person is trying to teach you C#, and it's mostly wrong for what we expect in C#

Use properties for get/set behavior instead, and name methods LikeThisWithAnInitialCaps

Really your class should be more like:

C#:
public class Persona
{
    public int Edad {get;set;}

    public Persona(int edad)
    {
        Edad = edad;
    }
}
I understand local as for example a variable that only exists inside a function.
 
Cool ! !
What I don't understand is how the GetEdad function generates the return of the edad variable, if between parentheses it never received anything?


C#:
Public void setEdad(int e)
 { edad=e; }

 Public int getEdad()
{ return edad;]
 
Return values for functions are not the same as parameters. Parameters are passed into a method (through the stack and/or CPU registers). Return values are passed out of a method (through a CPU register) back to the caller.

Code within a method can choose what it returns. It need not return a parameter that was passed in to it. Consider the following method:
C#:
public int GetOne()
{
    return 1;
}

Are you saying that GetOne() shouldn't work? Are you saying that the only way for it work would be:
C#:
public int GetOne(int value)
{
    return value;
}
and that I would have to call it by using GetOne(1)? So what happens if call GetOne(2048)?
 
What I don't understand is how the GetEdad function generates the return of the edad variable, if between parentheses it never received anything?

What do you think it has to receive? In your mind, how do you think methods (you call them functions) work?
 
and that I would have to call it by using GetOne(1)? So what happens if call GetOne(2048)?
This code shows 99 e . g. So if i can 2048 the compiler will show 2048
C#:
 int n = 2;

            Console.WriteLine( Hola(99) );
          
            static  int Hola(int z)
        {
             return z;
        }
 

Latest posts

Back
Top Bottom