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?

7
  • 1
    Instances don't have names, in general. What are you trying to accomplish? Commented Jan 31, 2015 at 19:51
  • 2
    Why do you want to do that? It seems like an awful idea. Commented Jan 31, 2015 at 19:51
  • What if you had Person steve = new Person(); Person joe = steve;? Commented Jan 31, 2015 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) Commented Jan 31, 2015 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. Commented Jan 31, 2015 at 19:56

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

It is possible, see: stackoverflow.com/questions/2566101/…. That doesn't mean it's a good idea in this case (or, ever, really...) of course.
@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.
@EdS that's a very different thing
@Cyral: Sure, I just assume it would be passed to the constructor or after the object is created.
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)

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.