0

I want to create a class in c#, that contains the instance name in an internal string. For example, the class 'Person':

Person steve = new Person();

and the class will look like that:

class Person
{
    private string Name;
}

So the string 'Name' will contain: "steve".

How can I do that? Is that possible?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • 1
    Instances don't have names, in general. What are you trying to accomplish? – John Saunders Jan 31 '15 at 19:51
  • 2
    Why do you want to do that? It seems like an awful idea. – Daniel Mann Jan 31 '15 at 19:51
  • What if you had `Person steve = new Person(); Person joe = steve;`? – John Saunders Jan 31 '15 at 19:52
  • I want to create a class called 'Airport', so I need astring with the airport name, and then I can download info about the specific airport. (It's not only that, it contains more methods) – user3057504 Jan 31 '15 at 19:54
  • Please edit your question to state more clearly what it is you want. As it stands, your question looks like you want a string which has the name of the variable. It makes it look like you think that instances have names just because they are assigned to a variable. – John Saunders Jan 31 '15 at 19:56
  • @JohnSaunders clearly OP *asks* for sample of using `Emit` to create method that uses particular variable name, call constructor of a class and assign property value :) - quite possible and should amaze OP for some time... (but it is very unlikely what OP *wants* to do - so very unclear). – Alexei Levenkov Jan 31 '15 at 20:03
  • @Alexei: If that's clear to you, then you are a better man than I :-) – John Saunders Jan 31 '15 at 20:05

3 Answers3

3

No, it's not possible.

Consider this:

Person steve = new Person();
Person john = steve;

Now what would Name be?

A variable name is not an instance name. Instances do not have names, but locations in memory, and several variable names might be pointing to the same instance.

Jcl
  • 27,696
  • 5
  • 61
  • 92
  • It *is* possible, see: http://stackoverflow.com/questions/2566101/how-to-get-variable-name-using-reflection. That doesn't mean it's a good idea in this case (or, ever, really...) of course. – Ed S. Jan 31 '15 at 23:02
  • @EdS. You can get the variable name of a known variable, but the class instance itself can not get the name of the variable it is assigned to itself. – Cyral Feb 01 '15 at 01:16
  • @EdS that's a very different thing – Jcl Feb 01 '15 at 08:29
  • @Cyral: Sure, I just assume it would be passed to the constructor or after the object is created. – Ed S. Feb 01 '15 at 11:43
3

What would happen if the instance created as a parameter? Or if it is used in an array? Or if it is reassigned to another variable? This is not a good idea (Nor is it possible the way you want it)

Use a string parameter in the constructor that assigns a value to the name:

class Person
{
    private string Name { get; set; }

    public Person(string name)
    {
         Name = name;
    }
}

And create the instance as:

Person steve = new Person("Steve");

The string "Steve" is now passed to the constructor of the class when it is created. See Constructors (C# Programming Guide)

Cyral
  • 13,999
  • 6
  • 50
  • 90
0

It is quite possible you are looking for Dictionary<string, Person> to map name of person to instance of a person:

class Person
{
    public string Name {get;set;}
}

var people = new Dictionary<string, Person>();

people.Add("Steve", new Person { Name = "Steve"});

var theSteve = people["Steve"];

Notes

  • as mentioned in all other answers name of the variable/field have no direct relation to values/properties of the instance.
  • making "Name" private looks somewhat strange - if you ever want to check what name of the person is you should make it public (or better make it property as shown in my sample). If you don't want other code to access "Name" - you'll need to set it in constructor.

It is also possible to do exactly what you are asking - "create method where there is variable with given name which holds instance of and object and one of private fields of the object has name equal to variable name". You'd use Reflection.Emit to construct such method at run-time and possibly need to use other APIs in Reflection namespace to alter value of private field... Also I doubt it is what you are looking for.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179