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:
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());
    }
}

In line 17 you created the reference to an object of type Greeter, right?
 
In line 17 you created the reference to an object of type Greeter, right?
What has that got to do with the topic of the thread? Stay focused on the Particular question… If you have a query about how to create new objects, start another thread
 
Back
Top Bottom