Forum Discussion

howie_'s avatar
howie_
Contributor
13 years ago

Referencing a variable's name

I'm trying to reference the name of  a variable, for logging. I'd like to do something like this (in JScript): 



var testVariable = 3;

Log.Message("Variable: " + testVariable.Name + "; value: " + testVariable);



Does anybody know how to do this? 



2 Replies

  • Hey,



    Couldn't you:



    Log.Message("Variable: testVariable; value: " + testVariable);



    As 
    testVariable is the name. As far as I know, the name of your variable "testVariable" won't change unless you do it manually so you might as well log it as a string.
  • On second look, what I'm really trying to do is this: 





    function LogVariable( variable)

    {

            Log.Message("Variable name: " + variable.Name + ", value: " + variable);

    }





    Call: 

    LogVariable(var1);

    LogVariable(var2);



    Output: 

    Variable name: var1, value: 1;

    Variable name: var2, value: 2;





    And I think my answer is "no, it can't be done that way", because I lose the name of the variable when it's contents are passed by value. I think what I'm going to have to do instead is this: 



    function LogVariable(variable, variableName)

    {

            Log.Message("Variable name: " + variableName + "value: " + variable);

    }



    Call: 

    LogVariable(var1, "var1");