Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get the variable name
#1
I want to retrieve and print each variable name from the parameters passed in a class. (My expression may not be clear enough, please refer to the comments in the code)

I searched for relevant information on Google and the suggestion I got was to use the callstack! However, I haven't been able to implement it yet. There might be a simpler method

I previously had a similar feature request in QM, but it was not resolved. 

Thank you in advance for any suggestions and Help
David
 
Code:
Copy      Help
// script "ClassA_Test.cs"
/*/ role exeProgram; outputPath %folders.Desktop%\ClassA; c ClassA.cs; /*/ //.
script.setup(trayIcon: true, sleepExit: true);
//..

string inp = "Hi";

string S1 = "hello world";

int I1 = 888;

string[] AS1 = { "hello", "world" };

ClassA.FuncA(inp, S1, I1, AS1);

Code:
Copy      Help
// class "ClassA.cs"

public class ClassA
{
    
    public static void FuncA(string inp, params object[] args)
    {

        print.it(inp);
        
        //Get the variable name passed as an argument to a function
        print.it($"fist Arg variable name: ?"); // ? -> It should output S1
        
        print.it($"Second Arg variable name: ??"); // ?? -> It should output I1
        
        print.it($"Third Arg variable name: ???"); // ??? -> It should output AS1
    }
}
#2
Code:
Copy      Help
// script "test ClassA.cs"
/*/ c \ClassA.cs; /*/

string inp = "Hi";

string S1 = "hello world";

int I1 = 888;

string[] AS1 = { "hello", "world" };

ClassA.FuncA(inp, [S1, I1, AS1]);

Code:
Copy      Help
// class "ClassA.cs"

public class ClassA
{
    
    public static void FuncA(string inp, object[] args, [CallerArgumentExpression("args")] string e_ = null)
    {

        print.it(inp);
        
        //print.it(e_);
        var k = e_[1..^1].Split(',', StringSplitOptions.TrimEntries);
        for (int i = 0; i < args.Length; i++) {
            print.it($"{k[i]} = {args[i]}");
            //print.it($"{k[i]} = {print.util.toString(args[i], true)}"); //this also prints array element etc, like print.it
        }
    }
}

It is the only possible way. With params not possible.
#3
thank you!
#4
What unique qualities does (params object[] args) possess, and can they be completely replaced by (object[] args) ?
#5
Read the C# params keyword documentation.
#6
Thank you!
ChatGPT:
Quote:The parameter `(params object[] args)` in C# allows a method to accept a variable number of arguments of type `object`, while `(object[] args)` simply represents an array of objects. 

The unique quality of `(params object[] args)` is that it allows the caller to pass a variable number of arguments directly to the method without explicitly creating an array. This makes calling the method more convenient, especially when the number of arguments can vary.

On the other hand, `(object[] args)` requires the caller to explicitly create an array and pass it to the method, which may be less convenient and less intuitive, especially when dealing with a variable number of arguments.

In terms of functionality, you could say that `(params object[] args)` can cover all scenarios where `(object[] args)` is used, but it provides the additional convenience of accepting a variable number of arguments without the need to explicitly create an array. However, if you are certain that the method will always receive an array of objects and not a variable number of arguments, then `(object[] args)` can be used without any issues.


Forum Jump:


Users browsing this thread: 1 Guest(s)