How to take the name of a variable within a method?

Asked

Viewed 1,283 times

1

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 wish it were:

nameToWrite

  • Hi, this is the website of stackoverflow in Portuguese, translate your question or also ask on the site in English

  • 2

    What is the reason for this? What do you really need?

  • You want to take the variable value or just the variable name?

  • I don’t think Reflection this must be possible, there is no link with the variable in the other method, even passing by reference

  • @Gabrielheming I am building a Generica class that interests me the name of the object directly past.

  • @Mayconf.Castro This question is indicated in the text, I only care about the name of the variable that is passed and not the internal, nor values

  • @Ricardopunctual Because, I have tried several ways and I can’t get to the name, even with reflecion and propertInfo. I can access the properties names of this object with Reflection but the name of this object I do not know.

  • @Pedromartins pq not get the name so var nomeVariavel = nameof(nameToWrite);? Note that the name of the variables can and will probably change after compiled.

  • You will not get, because variable names are not relevant from an operating point of view. Only on language and/or performance limitation issues.

  • It should be in line with what @Gabrielheming commented, and so when you "decompile" a code the variable names are not necessarily the same in the original code.

  • Note: the nameof solves the name at compile time (ie the name will be what you see and there is no resolution while running the application)

  • Apparently you want to go further. In addition to the excellent answers and comments: Take a look at the F#

  • @Tony what the F# has to offer in this?

  • It offers more resources.

Show 9 more comments

1 answer

4

You’re talking about local variables and these are just nicknames for memory addresses totally unnecessary for execution, it’s just an abstraction to facilitate the programmer’s understanding.

After compilation there is no variable name (local, in languages/implementations that are 100% native and static, even non-local symbols can be dropped), so there is nothing to access. If the exercise asks for exactly this, and I don’t think it does, which is misinterpretation, the exercise is flawed and makes no sense. If it’s not a drill, there’s something very wrong.

What you can do is put a parameter in the method and pass the name as string as argument for the method. Even this makes no sense.

Remembering that you always know the name of variables local during development, so it makes no sense to create a code to find out what the name is.

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

It makes no sense to know the name of the variable that was used to call the method, it has zero benefits in it. Nor should it have any. The only languages that make sense are the ones that have dynamic scope, which is one of the most idiotic ideas ever created in computing, and I can talk about that because I’ve worked with language like that, but luckily today it’s optional, and even when it was mandatory to do so it was better to pretend that it could not even take advantage of this.

So you don’t have to go by this name. And if you really needed to, you should justify it. Who knows is an XY problem (see next if you are reading this during Cup :D ). This method getName() is simply unnecessary and a programming error. So it may have some relevance, yet weird:

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

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

I put in the Github for future reference.

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

This is the same thing:

public A() {
    var nameToWrite = "thisdoesntmatter";
    WriteLine("nameToWrite"); //o nameof() daria mais proteção contra alteração desavisada
}
  • That answer is partly good, but unfortunately it does not fit into the scope of my development. The ideal option would be the class A using class B without knowing "nameof" or any other details. But what I see won’t be possible

  • We have no way of knowing its scope without you talking about it. And I doubt that’s possible, because it doesn’t make sense. has artificial requirements that do not solve anything. It is not that it is not possible, it is not necessary.

  • The scope is to create a generic class for programmers to use, the goal is to convert Object into data Structured string (a type made by me, is not XML nor JSON...) but it was interesting that when the programmer passes-if its object goes into the other class that would be an incognica, and it creates the string with the initial name of the variable.

  • Anyway, I’ve already arranged a work-Around for this issue.

  • It becomes increasingly clear that it is the wrong solution to the problem, or at least the wrong question to the need. You may have.

  • @Pedromartins what you call work-Around It’s actually called unnecessary gambiarra, because that’s not the problem at all. I even added some more to the answer, but that’s it, you have no reason to do what you want.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.