More than one condition for ternary operator inside file write function - page 2

 

I'm not quite understand about how ternary operator works and that's why I'm asking if it's possible to achieve what I want using ternary operator.

either way thanks for all the help.

 
Luandre Ezra #:
if it's possible to achieve what I want using ternary operator

"Is it possible to read a book upside down?" - It's possible, but very inconvenient.

The best way to avoid passing a lot of parameters is to pass an object (structure or class) by reference.

 
Vladislav Boyko #:

The best way to avoid passing a lot of parameters is to pass an object (structure or class) by reference.

One of the simplest ways:

struct STRUCT_PARAMS
  {
   double p1;
   double p2;
   double p3;
   double p4;
   double p5;
   double p6;
   double p7;
   double p8;
  };

void fill(STRUCT_PARAMS &obj, ENUM_TIMEFRAMES tf)
  {
   switch(tf)
     {
      case PERIOD_M5:  obj.p1=10.0; obj.p2=11.0; obj.p3=12.0; obj.p4=13.0; obj.p5=14.0; obj.p6=15.0; obj.p7=16.0; obj.p8=17.0;
         break;
      case PERIOD_M15:
      case PERIOD_M30: obj.p1=18.0; obj.p2=19.0; obj.p3=20.0; obj.p4=21.0; obj.p5=22.0; obj.p6=23.0; obj.p7=24.0; obj.p8=25.0;
         break;
      default:         obj.p1= 0.0; obj.p2= 0.0; obj.p3= 0.0; obj.p4= 0.0; obj.p5= 0.0; obj.p6= 0.0; obj.p7= 0.0; obj.p8= 0.0;
         break;
     }
  }

string toStr(const STRUCT_PARAMS &p)
  {
   return(StringFormat("||%f||%f||%f||%f||%f||%f||%f||%f||\n", p.p1, p.p2, p.p3, p.p4, p.p5, p.p6, p.p7, p.p8));
  }

void OnStart()
  {
   STRUCT_PARAMS params;
   fill(params, PERIOD_M15);
   Print(toStr(params));
  }

When using a switch, it will not be difficult for you to add another timeframe (unlike using the ternary operator):

void fill(STRUCT_PARAMS &obj, ENUM_TIMEFRAMES tf)
  {
   switch(tf)
     {
      case PERIOD_M5:  obj.p1=10.0; obj.p2=11.0; obj.p3=12.0; obj.p4=13.0; obj.p5=14.0; obj.p6=15.0; obj.p7=16.0; obj.p8=17.0;
         break;
      case PERIOD_M15:
      case PERIOD_M30: obj.p1=18.0; obj.p2=19.0; obj.p3=20.0; obj.p4=21.0; obj.p5=22.0; obj.p6=23.0; obj.p7=24.0; obj.p8=25.0;
         break;
      case PERIOD_H1:  obj.p1=26.0; obj.p2=27.0; obj.p3=28.0; obj.p4=29.0; obj.p5=30.0; obj.p6=31.0; obj.p7=32.0; obj.p8=33.0;
         break;
      default:         obj.p1= 0.0; obj.p2= 0.0; obj.p3= 0.0; obj.p4= 0.0; obj.p5= 0.0; obj.p6= 0.0; obj.p7= 0.0; obj.p8= 0.0;
         break;
     }
  }

This is one of the easiest ways. Then you can complicate this as you want (use methods/constructors/initializing sequences/return a pointer/...)

 

Here is the appropriate use of the ternary operator:

int max(int a, int b)
  {
   return(a > b ? a : b);
  }

This is better than like this:

int max(int a, int b)
  {
   if(a > b)
      return(a);
   else return(b);
  }

And using multiple ternary operators in one expression only brings unnecessary difficulties and inconvenience