Print variables values

 

I hope to write a function PrintVars that is able to print the print variables with their names. e.g.,

int var1=1;
bool var2=true;
int var3=2;

PrintVars(var1,var2,var3);

The output to the console is:

var1=1
var2=true
var3=2

The difficulty here is to

  1. Get the variable name;
  2. passing parameters of various types to the function.

Is there a way to achieve 1 and 2?

 
  1. Hope all you want, can't be done. You can't get variable names. Once you pass it, the function has a value not a name.
  2. int var1=1;
    bool var2=true;
    int var3=2;
    
    PrintFormat("var1=%i, var2=%s, var3=%i", 
                var1, 
                var2 ? "True" : "False", 
                var3);
    // var1=1, var2=True, var3=2


 
jackee1234:

I hope to write a function PrintVars that is able to print the print variables with their names. e.g.,

The output to the console is:

The difficulty here is to

  1. Get the variable name;
  2. passing parameters of various types to the function.

Is there a way to achieve 1 and 2?

#define PRINTVAR(v)     printf("%s = %s",#v,(string)v)


int var1=1;
bool var2=true;
int var3=2;

//PrintVars(var1,var2,var3);
PRINTVAR(var1);
PRINTVAR(var2);
PRINTVAR(var3);
 
Alain Verleyen:
#define PRINTVAR(v)     printf("%s = %s",#v,(string)v)
I never think about the stringizing operator since it isn't in the documentation.
 
MQL5: universal Print

Форум по трейдингу, автоматическим торговым системам и тестированию торговых стратегий

MetaEditor build 1463

fxsaber, 2016.11.10 10:42

class PRINTCLASS
{
public:  
  template <typename T>
  static void MyPrint( const T &Value )
  {
    T Array[1];
    
    Array[0] = Value;
    
    ::ArrayPrint(Array);
  }
  
  template <typename T>
  static void MyPrint( const T Value )
  {
    ::Print(Value);
  }
};

#define Print(A) PRINTCLASS::MyPrint(A)

void OnStart()
{
  MqlTick Tick;
  
  SymbolInfoTick(_Symbol, Tick);

  Print(Tick);
  
  int i = 5;
  
  Print(i);
}

Result

                 [time]   [bid]   [ask] [last] [volume]    [time_msc] [flags]
[0] 2017.05.09 19:36:20 1.08745 1.08749    0.0        0 1494358580466      96
5
 
Alain Verleyen:

Thanks, I learnt something new today 
Reason: