-1

So, this works:

class:

public class c_entry
{
    public string Name { get; private set; }

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

Create instance and send variable name as a parameter

public void method()
{
    c_entry c = new c_entry(nameof(c));
}

But what I really want to be able to do is set the Name by just referring to its instance name from inside the class so I don't have to pass the nameof(instance) as a parameter

public class c_entry
{
    public string Name { get; private set; }

    public c_entry()
    {
        Name = nameof(this); // This line won't compile because "this" expression doesn't have a name
    }
}

I don't think this is possible - but is it?

garfbradaz
  • 3,424
  • 7
  • 43
  • 70
jamheadart
  • 5,047
  • 4
  • 32
  • 63
  • Should not be possible as you can have as many names to your object as you like – Tim Oct 12 '18 at 08:34
  • 2
    What **exactly** do you want to pass the name of, if you could get the compiler to do it for you? The name of the surrounding member (`c_entry`)? The name of the variable (`Name` in that last piece of code and `c` in the former)? – Lasse V. Karlsen Oct 12 '18 at 08:36
  • 1
    The name of the surrounding member can be done using an attribute, see [CallerMemberNameAttribute](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.callermembernameattribute?view=netframework-4.7.2), but the name of the variable has no such trick. A different question would be why you think you need it? Why should the object be notified of the name of the first variable you stored a reference to/value of it into? Also, what if `c_entry c2 = c;`, now you have *two* variables, should this name be passed in as well? – Lasse V. Karlsen Oct 12 '18 at 08:38
  • At the point of construction, objects have no knowledge of how many references to that object may exist at any point in the future, nor when or how any of those references may be assigned to any particular named variables. – Damien_The_Unbeliever Oct 12 '18 at 08:39
  • I'm creating a list of `c_entry` with various properties, `Index`, `Name` and `Value` so I can iterate through them by `for i` loops, `foreach` loops and still be able to alter them. I also want to harness individual references to them in code. I've whittled a huge set of abstracts and arrays within arrays, and it works perfectly, I'm just trying to remove the double use of the name when instancing `c_entry` – jamheadart Oct 12 '18 at 08:44
  • Giving an object a Name property isn't odd, the fact that you're placing the name of the *variable* that refers to the object into that property **is**. Can you tell us why you're using **that name**? – Lasse V. Karlsen Oct 12 '18 at 08:47
  • 1
    "trying to remove the double use of the name" - No, `c` and `"c"` would be a double use of the name. `nameof(c)` is a perfectly refactor-safe single point of definition. – bommelding Oct 12 '18 at 08:51
  • Yes it's a single point of definition but if the instance, during creation has nameof(c) passed to it and assigned to variable `Name` - then it doesn't matter what the instance gets called later on, I just want to be able to find it in a list by its `Name` property – jamheadart Oct 12 '18 at 08:58
  • And what I mean by double use is that I have to write the variable twice - I'm just trying to make it easier to create many instances of it with the same effect `c_entry xxx = new c_entry()` instead of `c_entry xxx = new c_entry(nameof(xxx))` or `c_entry xxx = new c_entry("xxx")` – jamheadart Oct 12 '18 at 09:00

1 Answers1

4

No you cannot, because it does not make sense. An instance does not come with a single name.

var x = new YourClass(); // you might think this is named x

new YourClass(); // what's the name now? okay, maybe name should be nullable?

var x = new YourClass();
var y = x; // and now? 
           // What name should the single instance have now? 
           // should each instance have a list of names?
nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • It still depends on the requirement - that could be to use the target variable as a default. So two classes should be "x" and the middle one null. A string is already nullable. – bommelding Oct 12 '18 at 08:48
  • Yes it doesn't come with a single name and I might want to be able to refer to it with all its different names but I DEFINITELY want to be able to access it by iterating through a list of these objects and checking for initial instanced `Name` "c", which was set in the constructor when I created the instance. – jamheadart Oct 12 '18 at 08:57