0

Is it possible to get the declaration name of a class (dynamically) and pass it as a parameter in the constructor to set the name variable in the class itself?

Example:

public class Foo
{
    public string name;
    public Foo()
    {
        name = GetClassName();
    }
}

public class SomeOtherClass
{
    Foo className = new Foo();
    Console.WriteLine(foo.name);
}

As result I would expect it to write: "className".

halfer
  • 19,824
  • 17
  • 99
  • 186
Kyle Michiels
  • 25
  • 1
  • 1
  • 8
  • 1
    Do you want the class name "foo" or the variable name "className"? your example says variable name but you say class name – H.J. Meijer Apr 25 '18 at 08:34
  • `className` is not a "declaration name" (whatever that should be), but only the name of a _local_ variable. Local variable names have no meaning at run-time and they are no longer known (except for debugging purposes). – René Vogt Apr 25 '18 at 08:34
  • 1
    So you want an instance of the class to be named after the first field in which it is declared / stored? I don't think that is possible... Also how should this / is this going to work when the instance is not put into a field, for example when you just `return new Foo()` from some kind of factory method? – bassfader Apr 25 '18 at 08:34
  • 1
    Why should an instance of a class care for how a *reference* to it is named? That sounds really weird for my understanding. The name of a variable has abolsutely nothing to do with the object it references. – MakePeaceGreatAgain Apr 25 '18 at 08:37
  • This is a very similar (if not identical) question: [Finding the variable name passed to a function](https://stackoverflow.com/q/72121/3214843). – Manfred Radlwimmer Apr 25 '18 at 08:39
  • What should it print in the case of the reference to the object changing? for example _"Foo className = new Foo(); Foo className2 = className; className = null;"_ – PaulF Apr 25 '18 at 08:44
  • To answer your questions, when you just creat a new Foo it should be "" or nameless since it doesnt have a name. Why would I want the declaration name? Im working on a behaviour tree system in C# and you can create nodes. But instead of double typing like: Node countToThree = new Node("CountToThree"); I would like to set that name automatically so it's more user-friendly. – Kyle Michiels Apr 25 '18 at 09:12
  • See my answer. A **reference** has nothing to to with the **instance** it references. So the variable (which *is* a reference) means nothing to the actual instance. – MakePeaceGreatAgain Apr 25 '18 at 09:17

2 Answers2

3

No. That is not possible. There is no way to pass in a variable name without using a parameter.

This is the closest you can get:

Foo className = new Foo(nameof(className));
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
0

That sounds like a weird requirement. A variable is nothing but a reference to an object. The name of that reference has by no means anything to do with what this variable reference. Thus the actual referenced object doesn´t know anything about its references. In fact you may have even multiple references to the same Foo-instance. So how should the instance know to which variable you refer to? So what should happen in the following example:

var f = new Foo();
var b = f;

Now you have two references to the same instance of Foo. The instance can´t know which of hose is the right, unless you provide that information to it by using a parameter (e.g. to your constructor). The thing gets even worse if you have a factory creating your Foo-instance:

void CreateFoo()
{
    return new Foo();
}
// ...
var f = CreateFoo();

Now you have a further indirection, the constructor of Foo can surely not bubble though all layers in your call-stack until it reaches some assignement where it may get the actual name. In fact it´s possible that you don´t even assign your instance to anything - although this is merely a good idea:

CreateFoo();  // create an instance and throw it away

Anyway if you want to set a member of an instance to some value, you should provide that value to the instance. The answer by Patrick shows you how to do so.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • 2
    While I completely agree with what you said, this doesn't feel like an answer, more like an extended comment - especially because of the *"The answer by Patrick shows you how to do so."* – Manfred Radlwimmer Apr 25 '18 at 08:43
  • @ManfredRadlwimmer I agree with you, however it´s simply to long for an answer. In particular it shows OP how to solve his/her **actual** problem, which of course is hard to guess and *might* be an xy-problem. – MakePeaceGreatAgain Apr 25 '18 at 08:49
  • And it's without a doubt an important addition, I was just worried you might get NAA flagged but with the last edit that's now unlikely. – Manfred Radlwimmer Apr 25 '18 at 08:53
  • 2
    @ManfredRadlwimmer: "The question is unanswerable" is an answer in and of itself (paradoxes be damned). Or, at least, it's the only possible answer and the best OP can get. This answer is such an answer, with an elaboration as to why the question doesn't make sense to begin with. – Flater Apr 25 '18 at 08:53
  • @HimBromBeere Thanks for the reply and some explanation. What I want to achieve is simple, when you just create a new Foo it should be "" or nameless since it doesn't have a name. For your b = f example, b should have the same name as f in that case. Why would I want the declaration name? Im working on a behaviour tree system in C# and you can create nodes. But instead of double typing like: Node countToThree = new Node("CountToThree"); I would like to set that name automatically so it's more user-friendly – Kyle Michiels Apr 25 '18 at 09:16