2

I want the name of the defined class instance

For example i have this code :

MyClass hello = new MyClass();

And in MyClass i have a method :

MyClass{
    private string _name;
    .
    .
    public string getName(){
        return ...//THIS SHOULD RETURN 'hello'
    }
}

Is this possible to do ?

alix54
  • 444
  • 1
  • 5
  • 13
  • No, hello belong to the parent method and is not passed to the class constructor. – jdweng Mar 05 '17 at 13:47
  • From what I know it is not possible in C#. What are you trying to achieve? – Andrii Litvinov Mar 05 '17 at 13:47
  • 1
    And if I do `MyClass hello = new MyClass(); MyClass bye = hello` - what should it return then? – Evk Mar 05 '17 at 13:47
  • @Evk i will not do this with this class ... Anyway this should return hello then – alix54 Mar 05 '17 at 13:48
  • 3
    Just want to point that there is no such thing as "name of the defined class instance". Many variables with different names can point to the same instance. – Evk Mar 05 '17 at 13:50
  • @AndriiLitvinov I need that name to use for saving settings... i can do : `MyClass hello = new MyClass("hello");` , but i'm looking for faster method to do this – alix54 Mar 05 '17 at 13:50
  • 1
    It doesn't sound like a good design in C#. It you need this class to save setting I would better pass the name of the setting to it's constructor as you suggested. @Evk is right, and if you store the class in a collection, for instance what should it return then? – Andrii Litvinov Mar 05 '17 at 13:55
  • It seems to be not logic that a class will return the name of the instances. The class is unaware of the objects that instantiate it. – ehh Mar 05 '17 at 13:57
  • http://stackoverflow.com/questions/7369296/get-the-instance-name-of-this – nick Mar 05 '17 at 13:57

1 Answers1

-1

This is not possible other than:

MyClass hello = new MyClass();
string name = nameof(hello)

or

MyClass hello = new MyClass(nameof(hello));

class MyClass {
  private string Name;
  public MyClass(string name){ 
   this.Name = name; 
  }
  public string getName(){
    return this.Name;
 }
}

But this leads to problems like this:

MyClass hello = new MyClass(nameof(hello));
test = hello;
test.getName(); // returns: hello;

Depends on what exactly you want, I would suggests the following instead:

Variable name

nameof

The easiest way would be use nameof (as shown in the other answer):

var varName = new MyClass();
return nameof(varName); // will return "varName"

the great thing is this will compile time be converted to "varName" so you have the advantages of compile time checking but runtime speed of static string.

Disection of method

Another way to do it would be to disect the method and turn it into lambda experssion. In this lambda experssion you could search for the new variable declaration with the construction of this class. and then get the name of this variables. Im sorry but i have not much experience to show you an example for this. I would like you to refer to a duplicate post: Getting names of local variables (and parameters) at run-time through lambda expressions

Im not sure why you want to do this. since this is about how your code is named/programmed which doesn't realy matter during runtime. (then it only matters how it behaves.)

I would advice you to create an code analyzer instead for the roslyn compiler and add this one to your project.

Achitectural solution

The only way you would be able to get the behaviour you describe is to give the variable name to the class in the constructor:

var myVar = new MyClass(nameof(myVar));
var var2 = myVar;
var2.GetName(); // returns "myVar"

public class MyClass {
    public MyClass(name){
        this.createdWithName = name; 
    }
    public string GetName(){
        return this.createdWithName;
    }
}

classname (Not variable name!)

A more handy thing to want, would be to get the name of the class. and you can do that in the following ways:

nameof

return nameof(MyClass); //will compile to "MyClass"

So compile time you have the luxury of MyClass checking for types and for easy refactoring. But runtime it will always return 'MyClass'.

Reflection

return this.GetType().Name;

Will return return the name of this class. This will, compared to nameof, not always return 'MyClass', but actually return the reall class of the instance. For example when you have the subclass MySubClass this will return MySubclass.

Community
  • 1
  • 1
Joel Harkes
  • 10,975
  • 3
  • 46
  • 65
  • GetType().Name returns the Type name, not the instance name. – Tiago Freitas Leal Jul 27 '18 at 01:03
  • @TiagoFreitasLeal please read my whole answer! the first 3 headers clearly states 3 ways to get the variable name. There is no such thing as instance name (only if you have property called Name), only variable name! – Joel Harkes Jul 27 '18 at 04:56
  • The later is the correct answer: "There is no such thing as instance name (only if you have property called Name), only variable name! " So the correct question is "How to get teh variable name" and the answer is yes https://stackoverflow.com/questions/9801624/get-name-of-a-variable-or-parameter – Tiago Freitas Leal Jul 27 '18 at 12:32
  • This doesn't help in getting the instance name from inside the class. – Soham Dasgupta Dec 24 '18 at 08:01
  • 1
    @SohamDasgupta i'm providing options that are possible in c#. this is not possible in c# ill update my answer. – Joel Harkes Dec 27 '18 at 09:04