-2

I want to create a debug utility function to print a variable's name and value to the console, like this:

float my_variable = 10;
debug_print(my_variable);

And have the output be "my_variable = 10"

How would I be able to do this?

Tom Tetlaw
  • 83
  • 1
  • 10
  • 21

1 Answers1

2

You can use the nameof expression to get the name of a variable (though you have to pass the variable to it, so not sure how that's helpful since you already know the name), and then show the value:

Debug.Print($"{nameof(my_variable)} = {my_variable}");

Which is the same as:

Debug.Print($"my_variable = {my_variable}");

However, if this were put into a method, it would output the name of the method argument variable, not the name of the variable you pass in.

Rufus L
  • 36,127
  • 5
  • 30
  • 43