-1

Can't find out how to get element variable name from array.

Need something like that:

var test = 1;
var test2 = "2";

var array = new object[]{test, test2};
foreach (var v in array)
{
 Debug.Log($"{v.ToString()} {v}")
}

// test 1
// test2 2

I need that in one method, lets call it "void CombineWithName(params object[] msg){}

  • 1
    why do you need to know the variable name? – Lei Yang Feb 10 '22 at 08:23
  • @LeiYang for debug purposes – Кирилл Досын Feb 10 '22 at 08:24
  • 1
    maybe help: https://stackoverflow.com/questions/72121/finding-the-variable-name-passed-to-a-function – Lei Yang Feb 10 '22 at 08:25
  • 1
    There is no variable name in the array. You don't have an array of variables, you have an array of items. – ProgrammingLlama Feb 10 '22 at 08:27
  • @LeiYang thanks for the help. But my problem is that I'm trying to make a Logger which pass a (params object[] msg ) parameter to show the values to the console. In the end im getting "v" as a name and value. – Кирилл Досын Feb 10 '22 at 08:36
  • @LeiYang 's link has answers related to your question. – Xiang Wei Huang Feb 10 '22 at 08:40
  • `test` and `test2` are **variables**. As soon as you put these into an array the array itself has no information whatsoever where these values/references came from and to what they where assigned before ... inside the array best you can get is `index 0 = 1; index 1 = "2"` – derHugo Feb 10 '22 at 08:41
  • @derHugo sad. If I want to make that i can store reference to the variable name i should create my own solution? – Кирилл Досын Feb 10 '22 at 08:43
  • 1
    you would need e.g. `void Logger(params Tuple args)` and pass in an array like e.g. `new[] { new Tuple(nameof(test), test), new Tuple(nameof(test2), test2) };` or use a wrapper class (same concept) ... there isn't really any way to get the original variable and field names as soon as you only have a `object[]` – derHugo Feb 10 '22 at 08:58
  • @derHugo nah, thanks. I'm gonna write Logger messages too often with a different number. Not worth time. Thanks – Кирилл Досын Feb 10 '22 at 09:02

2 Answers2

4

This is impossible. The names test1 and test2 are not stored in the variable array but pointers to the location of the data. Using .net6 on .NETFiddle and the nameof method, the compiler tells you that the expression does not have a name.

.NETFiddle

derHugo
  • 83,094
  • 9
  • 75
  • 115
David Filler
  • 198
  • 7
0

The nameof operator allows you to get the name of a variable, type or member in string form without hard-coding it as a literal.

 var myVar = "ABC";
 Console.WriteLine(nameof(myVar));

Source: https://riptutorial.com/csharp/example/363/basic-usage--printing-a-variable-name

Edited:

In your above approach, since you are directly mapping variables to an object, There is no way to figure out the variable that was used to set a element, unless you save that information in a separate array or collection (For debug purpose)

var test = 1;
var test2 = "2";

var array = new object[]{test, test2};

var arrayOfArrays = {
    new {Name = nameof(test), Array=test}
,   new {Name = nameof(test2), Array=test2}
};
foreach (var p in arrayOfArrays) {
    Console.WriteLine(p.Name);
    ...
}
Syed M Sohaib
  • 307
  • 1
  • 8
  • The code example in Edited section doesn't work as the constructor of `arrayOfArrays` is wrong. Also the name `arrayOfArrays` is misleading since it's not an array of arrays, which doesn't help when `var` is being used. – Xiang Wei Huang Feb 10 '22 at 08:44
  • 1
    This doesn't compile ;) Of course you can do that .. but this doesn't really solve OP's attempt to have a `public void Logger(params object[] args){ /* log args with their names */ }` ... sure there are tons of ways if you manually pass in the names but I don't think this is really what OP wants. I'd say the answer is: What OP wants is not possible -> do something else. – derHugo Feb 10 '22 at 08:56