How do I get the name of a variable within a method?

0

I need to access the name of a variable within a method.

Example:

public class A
{
   public A()
   {
     string nameToWrite = "thisdoesntmatter";
     B.GetName(nameToWrite);
   }
}

public class B
{
   public static GetName(string nameIrrelevant)
   {
      Console.WriteLine(nameof(nameIrrelevant));
   }
}

But the return is this:

  

nameIrrelevant

And I wanted it to be:

  

nameToWrite

    
asked by anonymous 20.06.2018 / 13:06

1 answer

3

You're talking about local variables and these are only nicknames for memory addresses that are totally unnecessary to run, it's just an abstraction to make it easier for the programmer to understand.

After compilation there is no variable name (local, in languages / implementations that are 100% native and static , even nonlocal symbols can be discarded), so there is nothing to access . If the exercise asks exactly this, and I think it does not ask, which is wrong interpretation, the exercise is flawed and does not make sense. If it is not an exercise, there is something very wrong.

What you can do is put a parameter in the method and pass the name to string as argument to the method. Even this does not make sense.

Remembering that you always know the names of local variables during development, then it does not make sense to create a code to find out what the name is.

Even nameof() exists only as a protective measure to prevent the code from desynchronizing with the used text, so if you change the name of the variable, the compilation of nameof() will fail and you can manually change by agreeing that now the other name is correct. When you use the% direct% with the variable name, if you had string , and the variable changes to "x" , the code stays with y and may not be noticed. In practice this operator does nothing real in the application.

It does not make sense to know the name of the variable that was used to call the method, it has zero benefits in it. Nor should there be any. The only languages it makes sense are those with dynamic scope, which is one of the dumbest ideas ever created in computing, and I can speak well of it because I've worked with language like that, but luckily today it's optional, and even when it was obligatory to do so it was better to pretend that he could not take advantage of it.

Then you do not need to access this name. And if you needed it, you should justify it. Maybe it's an XY problem (see next if you're reading this during the Cup: D). This "x" method is simply unnecessary and a programming error. So it may have some relevance, still weird:

public class A {
    public A() {
        var nameToWrite = "thisdoesntmatter";
        B.WriteName(nameof(nameToWrite));
    }
}

public class B {
    public static WriteName(string name) => WriteLine(name));
}

Note that I changed the name of the method to make sense, and it should only exist as an abstraction.

This is the same thing:

public A() {
    var nameToWrite = "thisdoesntmatter";
    WriteLine("nameToWrite"); //o nameof() daria mais proteção contra alteração desavisada
}
    
20.06.2018 / 14:30