How do I go through an enumeration consistently? - page 2

 

And with a little more brainwork, you can get a function like this

int GetPeriodEnumerator(
uchar i,              // индекс перечисления 
ENUM_TIMEFRAMES &tf,  // сюда принимаем числовое значение таймфрейма 
string &tfs           // сюда принимаем строковое значение таймфрейма
)
  {
   switch(i)
     {
      case 0:  {tf=PERIOD_M1;tfs="M1";return(0);}
      case 1:  {tf=PERIOD_M2;tfs="M2";return(0);}
      case 2:  {tf=PERIOD_M3;tfs="M3";return(0);}
      case 3:  {tf=PERIOD_M4;tfs="M4";return(0);}
      case 4:  {tf=PERIOD_M5;tfs="M5";return(0);}
      case 5:  {tf=PERIOD_M6;tfs="M6";return(0);}
      case 6:  {tf=PERIOD_M10;tfs="M10";return(0);}
      case 7:  {tf=PERIOD_M12;tfs="M12";return(0);}
      case 8:  {tf=PERIOD_M15;tfs="M15";return(0);}
      case 9:  {tf=PERIOD_M20;tfs="M20";return(0);}
      case 10: {tf=PERIOD_M30;tfs="M30";return(0);}
      case 11: {tf=PERIOD_H1;tfs="H1";return(0);}
      case 12: {tf=PERIOD_H2;tfs="H2";return(0);}
      case 13: {tf=PERIOD_H3;tfs="H3";return(0);}
      case 14: {tf=PERIOD_H4;tfs="H4";return(0);}
      case 15: {tf=PERIOD_H6;tfs="H6";return(0);}
      case 16: {tf=PERIOD_H8;tfs="H8";return(0);}
      case 17: {tf=PERIOD_H12;tfs="H12";return(0);}
      case 18: {tf=PERIOD_D1;tfs="D1";return(0);}
      case 19: {tf=PERIOD_W1;tfs="W1";return(0);}
      case 20: {tf=PERIOD_MN1;tfs="MN1";return(0);}
     }
   return(-1);
  }
 
sergey1294:

And if you rack up your brains a bit more you can get the following function

Why

return(0)

Since the function is int, let return do the trick too.

int GetPeriodEnumerator(
uchar i,              // индекс перечисления 
ENUM_TIMEFRAMES &tf,  // сюда принимаем числовое значение таймфрейма 
string &tfs           // сюда принимаем строковое значение таймфрейма
)
  {
   switch(i)
     {
      case 0:  {tf=PERIOD_M1;tfs="M1";return(PeriodSeconds(tf)/60);}
      case 1:  {tf=PERIOD_M2;tfs="M2";return(PeriodSeconds(tf)/60);}
      case 2:  {tf=PERIOD_M3;tfs="M3";return(PeriodSeconds(tf)/60);}
      case 3:  {tf=PERIOD_M4;tfs="M4";return(PeriodSeconds(tf)/60);}
      case 4:  {tf=PERIOD_M5;tfs="M5";return(PeriodSeconds(tf)/60);}
      case 5:  {tf=PERIOD_M6;tfs="M6";return(PeriodSeconds(tf)/60);}
      case 6:  {tf=PERIOD_M10;tfs="M10";return(PeriodSeconds(tf)/60);}
      case 7:  {tf=PERIOD_M12;tfs="M12";return(PeriodSeconds(tf)/60);}
      case 8:  {tf=PERIOD_M15;tfs="M15";return(PeriodSeconds(tf)/60);}
      case 9:  {tf=PERIOD_M20;tfs="M20";return(PeriodSeconds(tf)/60);}
      case 10: {tf=PERIOD_M30;tfs="M30";return(PeriodSeconds(tf)/60);}
      case 11: {tf=PERIOD_H1;tfs="H1";return(PeriodSeconds(tf)/60);}
      case 12: {tf=PERIOD_H2;tfs="H2";return(PeriodSeconds(tf)/60);}
      case 13: {tf=PERIOD_H3;tfs="H3";return(PeriodSeconds(tf)/60);}
      case 14: {tf=PERIOD_H4;tfs="H4";return(PeriodSeconds(tf)/60);}
      case 15: {tf=PERIOD_H6;tfs="H6";return(PeriodSeconds(tf)/60);}
      case 16: {tf=PERIOD_H8;tfs="H8";return(PeriodSeconds(tf)/60);}
      case 17: {tf=PERIOD_H12;tfs="H12";return(PeriodSeconds(tf)/60);}
      case 18: {tf=PERIOD_D1;tfs="D1";return(PeriodSeconds(tf)/60);}
      case 19: {tf=PERIOD_W1;tfs="W1";return(PeriodSeconds(tf)/60);}
      case 20: {tf=PERIOD_MN1;tfs="MN1";return(PeriodSeconds(tf)/60);}
     }
   return(-1);
  }

and the call is probably like this

   ENUM_TIMEFRAMES tf;
   string tfs;
   Print(" мин=",GetPeriodEnumerator(11,tf,tfs),"  ENUM=",tf,"  string=",tfs);

the result is

 мин=60  ENUM=16385  string=H1
 
On enumerations, there is a discussion:
1) introducing ++ for enumeration
2) conversion of enumeration to string
ENUM_TIMEFRAMES t=PERIOD_M1;
string s=(string)t;

s будет иметь значение "PERIOD_M1"
 
mql5:
On enumerations, there is a discussion:
1) introducing ++ for enumeration
2) conversion of enumeration to string

Strongly support. Especially the first point. There are hundreds of enumerations in the standard package alone, and it is too expensive to write a separate function for each enumeration.
 
Ilyas:
On transfers, there is a discussion:
1) introducing ++ for enumeration


Is this functionality already in place?

Or another alternative to list items sequentially?

 
Give us iterators!
 

It seems to me that incrementing an enumeration is not the right step.

The default enumeration does not necessarily have to be sequential, and besides, during development - values can be added to it "in the middle".

Of course, to "simplify" it would be nice to get immediate "next" value by incrementing enumeration, but in my opinion, this is a potentially dangerous practice.

To get the "next value" in the enumeration, the most reasonable thing is to use special function, as already mentioned here.

And people seem to ignore the default selector, and in vain. Every switch() must contain this selector with a warning that an obscure value has arrived.

 

George Merts:

by incrementing enumerations - getting the "next" value immediately

That's whatIlyas meant.

I had a question for him or the developer in charge. Has this problem been solved - somehow to go through all the elements programmatically or is it still in the process...

I don't really care how to pass

foreach operator

new functions like GetNextEnum / GetLastEnum / GetFirstEnum

or overload ++

as long as it is not as was suggested above - to draw functions for current denominations and then be afraid that nothing will break when they are updated by developers

 

God, why such complexities - some plussers, iterators... The most obvious and flexible solution is to introduce a function in the language that gets all enumeration values into an array. And then you decide how to traverse this array, even crosswise.Besides, you're going to have to save all the values anyway, since you're not going to re-hash everything on every call. Once you've bypassed it, stored it, and then you just take it from the array. So isn't it easier to get a ready array at once?

By the way, the variant with operator overloading ++ may cause incorrect work of existing programs, so it is an extremely bad way.

Well, the examples shown here (case 1: return value1; case 2: return value2; case 3: return value3... An adequate person would put all the values into an array and just get the needed value by its index, while for the reverse task he/she will use binary search.

 
Alexey Navoykov:

God, what's with all the complexity - the pluses, the iterators...

I don't care how you want it to be.

Well, the examples shown here (case 1: return value1; case 2: return value2; case 3: return value3... An adequate person would put all the values into an array and just get the needed value by its index, while a binary search will be used for the reverse task.

switch is a very effective thing