1
votes

I hope to write a function PrintVars() that is able to print the variables altogether with their names. e.g.:

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

PrintVars( var1, var2, var3 );

The expected 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

1 Answers

0
votes

Neither 1) nor 2)

as MQL4 is a strong typed, compiled language.


Other possibilities to escape from this language syntax constraint ?

One may design a middle-ware tool for doing this, either locally, inside MQL4 or externally, via a messaging based distributed system ( having a remote terminal window ).

MQL4-based polymorphism is somewhat easier with class-based calling interface, while the efforts would be immense if such module ought be elaborated for some production grade deployment.

Somewhat clumsy sketch of the approach follows:
- create your vars as a properly configured polymorphic instance of aNamedVARIABLE class
- create a printing function, receiving a long list ( or a placeholder ( string &NameValueARRAY[] ) for a string array[][2]; initialised with "" and pre-filled with Name-Value pairs by .getName2PRINT(), resp. .getValue2PRINT() public methods ) of string variables to print, unless they are left as just initialised == ""

string printFun( const string N1 = "",
                 const string V1 = "",
                 const string N2 = "",
                 const string V2 = "",
                 ...
                 ..
                 .
                 ){ string aPrintSTRING  = "";
                           aPrintSTRING += ( ( N1 != "" ) ? ( N1 + " = " + V1 ) : "" );
                           aPrintSTRING += ( ( N2 != "" ) ? ( N2 + " = " + V2 ) : "" );
                           aPrintSTRING += ( ( N3 != "" ) ? ( N3 + " = " + V3 ) : "" );
                           ...
                           ..
                           .
                 return(   aPrintSTRING );
                 }
...
..
.
print( printFun( aNamedVAR1.getName2PRINT(), aNamedVAR1.getValue2PRINT(),
                 aNamedVAR2.getName2PRINT(), aNamedVAR2.getValue2PRINT(),
                 aNamedVAR3.getName2PRINT(), aNamedVAR3.getValue2PRINT(),
                 aNamedVAR4.getName2PRINT(), aNamedVAR4.getValue2PRINT()
                 )
       );

class aNamedVARIABLE{
      private:
              string aNameAsSTRING    = "";

              bool   aValueAsBOOL      = False;
              bool   aValueAsBOOL_[]   = {};
              int    aValueAsINT       = EMPTY_VALUE;
              int    aValueAsINT_[]    = {};
              string aValueAsSTR       = "";
              string aValueAsSTR_[]    = {};
              ...
              ..
              .
              isBoolean     = False;
              isInteger     = False;
              isShort       = False;
              isUint        = False;
              isUchar       = False;
              isChar        = False;
              isString      = False;
              isDouble      = False;
              isDatetime    = False;
              isColor       = False;
              isClass       = False;
              isEnum        = False;
              isStruct      = False;
              isBooleanArr  = False;
              isIntegerArr  = False;
              isUintArr     = False;
              isShortArr    = False;
              isUintArr     = False;
              isUcharArr    = False;
              isDoubleArr   = False;
              isDatetimeArr = False;
              isColorArr    = False;

      public:
              aNamedVARIABLE() {};                // default constructor
              aNamedVARIABLE( const string aName,
                              const bool   aBool ){  aNamedVARIABLE::setBool( aBool );
                                                     aNamedVARIABLE::setName( aName );
                                                     }
              aNamedVARIABLE( const string aName,
                              const int    anInt ){  aNamedVARIABLE::setInt(  anInt );
                                                     aNamedVARIABLE::setName( aName );
                                                     }
              ...
              ..
              .
              getName2PRINT() {                      return( this.aNameAsSTRING );
                                                     }
              getValue2PRINT() {                     if ( this.isBoolean ) return( this.aValueAsBOOL ? "True" : "False" );
                                                     if ( this.isInteger ) return( str( this.aValueAsINT ) );
                                                     ...
                                                     ..
                                                     .
                                                     }
      }