How can I turn an int to a constant ?

 

I want to use differents Timframe into a swicht, but program ask for a constant


This do not work : constant expression required

int tf1 = 5;
int tf2 = 30;
int tf3 = 240;

calcul( tf2 );

double cacul( int tf )
 {
   Switch( tf )
     {
      case tfi :
      case tf2 : 
      case tf3 : 
     }
return(0);
}

 
 
Obviously you can't. Variables aren't constants.
int tf1 = 5;
int tf2 = 30;
int tf3 = 240;

calcul( tf2 );

double cacul( int tf ){
   if(     tf == tf1) ...
   else if(tf == tf2) ...
   else if(tf == tf3) ...
   else /* default */
   return(0);
}
 
ffoorr:

I want to use differents Timframe into a swicht, but program ask for a constant


If tf1, tf2, tf3 don't change, you could do this:

#define tf1 5
#define tf2 30
#define tf3 240

double calcul( int tf )
 {
   switch( tf )
     {
      case tf1 :
      case tf2 : 
      case tf3 : 
        break;
     }
        return(0);
}

void OnStart()
{
    calcul( tf2 );
}

There are reasons NOT to do this (see the third answer in stackoverflow post), but since "const int" doesn't work for MQL switch statements, I do admit sometimes doing this for switch() statements.

Why would someone use #define to define constants?
Why would someone use #define to define constants?
  • stackoverflow.com
It's simple question but why would someone use #define to define constants?
 
whroeder1:
Obviously you can't. Variables aren't constants.
Thank this is what I have done