1

I'm creating a bunch of string variables, which are to be used to store the value of the variable name itself. e.g

string name = "name";
string number="number;

but as the list gets long, it becomes tedious to repetitively typing everything, so I made a class that just holds string data, and let the constructor to do the dirty work. e.g

class Data
{
public Data()
{
//pseudo code here:
name = NameOfTheVariable(this);
}
public string name {get;set;}    
}

So the ideal output would be something like this:

public static void Main()
{
//Declare an object here, the constructor will automatically assign the 
//variable name to the property.
 Data number = new Data();
 Console.WriteLine(number.name);
}

The output would be: "number", which is the variable name itself.

I did try to use MemberExpression mentioned in this post: get name of a variable or parameter

But the function doesn't take the keyword "this" as an valid argument. so I cannot use the function provided in that post, eg:

class Data
{
public Data()
{
//This line will not work
name= MemberInfoGetting.GetMemberName(() => this);
}
public string name{get;set;}

}

Anybody know how I could solve this problem?

Skepti_Capy
  • 107
  • 7
  • 1
    This is kinda complicated and weird way. You should create static class and declare your variables as static fields there. – kir.gera Nov 13 '17 at 07:30
  • 3
    I am wondering what you need this for? You can get variable/property names through reflection. But that's kinda slow. Also, in recent versions (not sure when it was introduced exactly) of c# there is `nameof()` ... – Fildor Nov 13 '17 at 07:34
  • So you are working with C# version where `nameof` is not available? – Evk Nov 13 '17 at 07:35
  • 3
    @HenkHolterman `string name = nameof(name);` works fine for me. – Evk Nov 13 '17 at 07:47
  • Rerlated: https://stackoverflow.com/q/23959398/ – H H Nov 13 '17 at 07:49
  • There's so much here I don't understand. First, why is you class not a `static` class? Second, why do you even _have_ this class? Wouldn't a table of string resources in the assembly be better? Third, why can't you just declare each property as e.g. `public string Name { get; } = nameof(Name);`? If that's really too much typing, I guess you could use reflection, to enumerate all of the class's properties and for any of the `string` type, set the value to the property's name. What _specifically_ are you haven't trouble with here? – Peter Duniho Nov 13 '17 at 07:55
  • 1
    _"which is what the OP seems to want"_ - Exactly this is pretty *un*clear at least to me. @HenkHolterman I am not even sure if OP *means* "variable" when they say "variable" ... – Fildor Nov 13 '17 at 08:13
  • What about a static class which holds all your variables, and at runtime you iterate through them (reflection) and initialize them, and possibly passing them to some constructor? – HelloWorld Nov 13 '17 at 08:26
  • @C.Da do it the way you want is not possible, so you have to implement any of alternatives. – Evk Nov 13 '17 at 08:33

1 Answers1

0
Data number = new Data();
Console.WriteLine(number.name);

In C# 6.0 and above that would be:

SomeNumberClass number = new SomeNumberClass();
Console.WriteLine(nameof(number));

The solution for Pre-C# 6 you linked would work accordingly, you just would not write nameof but use a static method instead:

SomeNumberClass number = new SomeNumberClass();
Console.WriteLine(MemberInfoGetting.GetMemberName(() => number));
Fildor
  • 14,510
  • 4
  • 35
  • 67
  • but if i wanna do it in the constructor, i can't assign variable name as value because i cannot use 'this' keyword in the argument – Skepti_Capy Nov 13 '17 at 18:20
  • @C.Da You don't do this in the CTOR of `Data`. I just left Data here because you used it in your examples. The last snippet would just be called instead of the line `Console.WriteLine(nameof(number));` when using a C# version prior to C# 6.0. – Fildor Nov 14 '17 at 07:53