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:
Above we can clearly see that if it does not receive any parameters, the function called Hello does not work, however the set method contradictorily does work and doesnt generate two errors.
 
This works correctly:
C#:
using System;
                   
public class Program
{
    static int n = 2;
   
    public static void Main()
    {
        Console.WriteLine(Hola());
    }

    public static int Hola()
    {
        return n;
    }
}

 
This works correctly:
C#:
using System;
                  
public class Program
{
    static int n = 2;
  
    public static void Main()
    {
        Console.WriteLine(Hola());
    }

    public static int Hola()
    {
        return n;
    }
}

I tried it and it worked ok, incredible. Could it be because it is in a class?
 
This code generates twos errors: error CS1501 No overload for method 'method' takes 'number' argument and error CS8421 An expression tree may not contain a reference to a local function
The fact that it's talking about local functions suggests that you are target .NET 6 or later and using top-level statements without understanding how they work and what the implications are. Maybe you should learn. There's a link provided in the code when you create such a project. Alternatively, check the box to not use top-level statements when you create the project.
 
C#:
int n = 2;

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

This code generates twos errors: error CS1501 No overload for method 'method' takes 'number' argument and error CS8421 An expression tree may not contain a reference to a local function
Why would you expect it to work?

You call Hola passing in one argument:

C#:
    Hola(n);

But you defined Hola as taking zero arguments (nothing between the () :

C#:
[QUOTE]
static  int Hola()
[/QUOTE]

C# uses the position and types of arguments to work out which method you want to call. Unless a parameter is optional, you have to supply it. If you supply too many parameters or the wrong type, C# won't be able to find the method you want to call

It's like writing "24 Acacia Avenue" on a letter and putting it in the post, when acaci Avenue is under construction and there aren't any houses on it yet. What would you like the postman to do with your letter?
 
The fact that it's talking about local functions suggests that you are target .NET 6 or later and using top-level statements without understanding how they work and what the implications are. Maybe you should learn. There's a link provided in the code when you create such a project. Alternatively, check the box to not use top-level statements when you create the project.
I assume you mean this link, really?
 
This works correctly:
C#:
using System;
                
public class Program
{
    static int n = 2;
 
    public static void Main()
    {
        Console.WriteLine(Hola());
    }

    public static int Hola()
    {
        return n;
    }
}

I understand the metaphor of the postman and the street... but I don't understand it here in C#.
In this post after hello it also has its two () empty. Forr examplo line 12.
 
In post #19 everything is consistent. Hola() on line 12 expects no parameters. So the call on line 9 does not pass any parameters.

The return statement on line 14, returns the value of [static] class variable n declared and initialized on line 5. n has to be static for it to be accessible to the static method Hola(). Hola() was declared to be static on line 12.
 
As per @cjard 's excellent recommendation. Post #19 re-written to not use static:

C#:
using System;

public class Greeter
{
    private int n = 2;
   
    public int Hola()
    {
        return n;
    }
}

public class Program
{
    public static void Main()
    {
        var greeter = new Greeter();
        Console.WriteLine(greeter.Hola());
    }
}

 
The fact that it's talking about local functions suggests that you are target .NET 6 or later and using top-level statements without understanding how they work and what the implications are. Maybe you should learn. There's a link provided in the code when you create such a project. Alternatively, check the box to not use top-level statements when you create the project.
In other words, what you mean is that I should have created the function outside the [], because this function is inside the [] of the Main or did I misunderstand?
 
You need to stop using us as a substitute for your own research. Stop what you're doing and read about top-level statements and local functions. Get a broad understanding of the subject first and then ask about specific details you don't understand. When you use top-level statements, every function is a local function. If you already understand local functions then you understand the implications of that. Either learn about top-level statements and local functions and use them properly or don't use top-level statements. It's not a problem if you don't use them. Some people - even in this thread - think that they're a solution in search of a problem. They're supposed to enable you to write less code but it's not much less anyway and if doing so causes confusion then just avoid them, which you can do by simply checking a box when you create the project.
 
I don't understand it here in C#.
In this post after hello it also has its two () empty. Forr examplo line 12.
and that code you posted, it works.. But it is different code to the one that doesn't work

Here is a code that doesn't work:
C#:
class X{
    static void Main(){
      Hola(1);
    }

    void Hola(){
    }
}

Here is a code that works:

C#:
class X{
    static void Main(){
      Hola(1);
    }

    void Hola(int x){
    }
}

The first code doesn't work because it tries to pass a number argument to a method that does not accept any arguments

Here is another code that doesn't work:

C#:
class X{
    static void Main(){
      Hola("a", 1);
    }

    void Hola(int x, string y){
    }
}

The number of arguments is correct: Hola accepts two arguments and the main tries to call it with two arguments, but they are in the wrong order; you can't pass a string as an int and an int as a string. C# won't reorder the arguments when passing like this because the position is vital.

This code works:

C#:
class X{
    static void Main(){
      Hola("a", 1);
    }

    void Hola(int x, string y){
    }
   
    void Hola(string y, int x){
    }
}

There are two same name methods. C# can know which one to use based on the arguments passed. It will use the Hola(string y, int x) one because that is the order of the arguments that Main calls: Hola("a", 1);

This code also works:



C#:
class X{
    static void Main(){
      Hola(y: "a", x: 1);
    }

    void Hola(int x, string y){
    }
}

When Hola is called the arguments are passed in a different order but you see the name of the argument is put first. It doesn't now matter that the order is wrong because c# has been told the name of the argument as well as the value. This means it can out the correct value in the right place anyway
 

Latest posts

Back
Top Bottom