How to pass ENUM values to Array?

 
I am very new to MQL4

How to pass ENUM values to array -  ENUMDataInput

How to programmatically count ENUM size (so I don't have defined ENUMSize=5) ?

Thanks for the help.

enum MALines{_20=1,_34=2,_50=3,_100=4,_200=5};

int ENUMSize=5;

void LinesArray(string ENUMDataInput, int ENUMSizeInput)
{
   int LinesArray[];
   ArrayResize(LinesArray,ENUMSizeInput,ENUMSizeInput);
  
        for (int i=1; i <= ENUMSizeInput; ++i) {
             LinesArray[i-1] = StrToInteger( EnumToString(ENUMDataInput(i)) ) ;
        }  
}


//--- LinesArray should be contains values 20,34,50,100,200


 
wieb:
I am very new to MQL4

How to pass ENUM values to array -  ENUMDataInput

How to programmatically count ENUM size (so I don't have defined ENUMSize=5) ?

Thanks for the help.

enum MALines{_20=1,_34=2,_50=3,_100=4,_200=5};

int ENUMSize=5;

void LinesArray(string ENUMDataInput, int ENUMSizeInput)
{
   int LinesArray[];
   ArrayResize(LinesArray,ENUMSizeInput,ENUMSizeInput);
  
        for (int i=1; i <= ENUMSizeInput; ++i) {
             LinesArray[i-1] = StrToInteger( EnumToString(ENUMDataInput(i)) ) ;
        }  
}


//--- LinesArray should be contains values 20,34,50,100,200



Try reading this thread.

You may decide you want to change your approach, and if not there are some examples in there too.

H‌TH

 
honest_knave:


Have a read of this thread.

You may decide you want to change your approach, and if not there are some examples in there too.

H‌TH


Thank you for very fast reply

Hope this approach solve my problem - https://www.mql5.com/en/forum/83666/page4#comment_2855538 

enum XXX {x1 = -10, x2 = 0, x3 = 11};

template<typename E>
int EnumToArray(E dummy, int &values[], const int start = INT_MIN, const int stop = INT_MAX)
{
  string t = typename(E) + "::";
  int length = StringLen(t);
  
  ArrayResize(values, 0);
  int count = 0;
  
  for(int i = start; i < stop && !IsStopped(); i++)
  {
    E e = (E)i;
    if(StringCompare(StringSubstr(EnumToString(e), 0, length), t) != 0)
    {
      ArrayResize(values, count + 1);
      values[count++] = i;
    }
  }
  return count;
}

int OnInit()
{
  Print("ENUM elements:");
  XXX a;
  int result[];
  int n = EnumToArray(a, result, -100, 100);
  Print("Count=", n);
  for(int i = 0; i < n; i++)
  {
    Print(i, " ", EnumToString((XXX)result[i]), "=", result[i]);
  }
  
  return 0;
}

/‌/Output ENUM elements:

//Count=3

//0 x1=-10

//1 x2=0

//2 x3=11

/‌/It's important to specify reasonable values for parameters start and stop , because the cycle from minimal to maximal integer value (which is used by default when the parameters are skipped) executes tooooo slow taking into account that string functions are used inside.

 

What about this way:

enum XXX {x1 = -10, x2 = 0, x3 = 11, SizeXXX=3};
double arr[];
..
int OnInit() {
   ...
   ArrayResize(arr,SizeXXX);
   ...
}

If you change the elements of XXX you are already there to change SizeXXX as well.

 
Carl Schreiber: What about this way:
enum XXX {x1 = -10, x2 = 0, x3 = 11, SizeXXX=3};

If you change the elements of XXX you are already there to change SizeXXX as well.

This is my approach as well, except:
  1. I use count not size (enums are all 4 bytes in size, arrays have variable size.)
  2. I don't make it part of the enum. (Don't want inputs to be able to select the count.)
  3. If the values are consecutive, then I also define first and last, so they can be used in loops.
    enum XXX {x1, x2, x3}; #define XXX_FIRST  x1
                           #define XXX_LAST   x3
                           #define XXX_COUNT (XXX_LAST-XXX_FIRST+1)
  4. If they are not, then I create next/prev functions.
 
wieb:
enum MALines{_20=1,_34=2,_50=3,_100=4,_200=5};

//--- LinesArray should be contains values 20,34,50,100,200
Just define conversion functions.
enum MALines{_20=1, _34, _50, _100, _200}; #define MALines_FIRST _20
                                           #define MALines_LAST  _200
const int MALinesPeriods[] = {20, 34, 50, 100, 200};
int     MALines_to_period(MALines e){ return MALinesPeriods[e]; }
MALines period_to_MALines(int p){
   for(MALines e = MALines_FIRST; e < MALines_LAST; ++e)
      if(p == MALinesPeriods[e]) return e;
}

Note, you should use self-documentating variable names (_50 says nothing.)

The enum constants must be unique, you can't have two _50's. MAperiod_34 would be better.

Leading underscore is for system defined things (e.g. _Symbol) Don't use leading underscores.‌

finally, why don't use just make the value equal to the name? Then you don't need the conversion functions.

enum MALines{MAperiod_20=20, MAperiod_34=34, MAperiod_50=50, MAperiod_100=100, MAperiod_200=200}; #define MALines_COUNT 5