Download MetaTrader 5

Array out of range / ArrayResize problem (yes again im sorry)

To add comments, please log in or register
Do you want to become a well-known developer? Execute the orders of traders in the Freelance service!
Jason Crossfield
155
Jason Crossfield 2015.12.11 16:32 

Ok.  Im at the end of my patience and am now putting my problem to the masses for support...... please.

The following test code is to solve a problem I am experiencing with a multi dimensional array.  The first problem is that the ArrayResize function is not resizing the array (note my print result).  Secondly I have the array out of range error on first loop pass at [50,11].

#property strict

string Opened[][12],Closed[][9];

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetTimer(60);
      
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
      
  }

//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---

   int opencnt=0;
   int closecnt=0;
   
   for(int i=0;i<=10;i++) 
    { 
    
    closecnt=closecnt++;
    ArrayResize(Closed,closecnt);
    Print("ClosedSize = ",ArrayRange(Closed,0)); // prints 0 (but should be 1 after ArrayResize?)
// the following line is [50,11]
    Closed[closecnt][0]="one";
    Closed[closecnt][1]="two";    
    
    opencnt=opencnt++;
    ArrayResize(Opened,opencnt);
    Print("OpenedSize = ",ArrayRange(Opened,0));
    Opened[opencnt][0]="one";
    Opened[opencnt][1]="two";
    
    }
    
    Print("Closed[0] = ",Closed[0][0]);
    Print("Closed[8] = ",Closed[8][1]);
    Print("Opened[0] = ",Opened[0][0]);
    Print("Opened[8] = ",Opened[8][1]);
   
  }

 

Any help is much appreciated, thanks. 

Carl Schreiber
6609
Carl Schreiber 2015.12.11 16:58  
    closecnt=closecnt++;
    ArrayResize(Closed,closecnt);

1) closecnt++ means closecnt is incremented AFTERWARDS but ++closecnt increments closecnt BEFORE the order is executed.

2) You don't need closecnt=closecnt++ or closecnt=++closecnt!  closecnt++ or ++closecnt is all you need!

3) In your case you were able to write:

ArrayResize(Closed,++closecnt); // deleting this: closecnt=closecnt++;!!
Jason Crossfield
155
Jason Crossfield 2015.12.11 17:08  
Thanks gooly for your help.  You have most definitely solved my ArrayResize problem which is now Printing 1 rather than 0.   However the Array out of range error still remains at the same position.  I have even attempted to set the this to a static array with the same error... ?  
Jason Crossfield
155
Jason Crossfield 2015.12.11 17:12  
Additionally it is good advise regarding the increment issue ad the ++ positioning on the variable.  I was unaware of this and is great information for future use.   Thanks so very much!
Carl Schreiber
6609
Carl Schreiber 2015.12.11 17:34  

1) where do you get this error?

2) Use Comment(__LINE__,..) to see the size and the request right before any array request as the last Comment(..) before the crash will remain on screen.

Jason Crossfield
155
Jason Crossfield 2015.12.11 18:06  

Sorry gooly can you give me an example of a comment line to request.. mql4 doc for is limited and states array can not be passed in comment function..? thanks

Jason Crossfield
155
Jason Crossfield 2015.12.11 18:35  

A comment pre and post ArrayResize is all see that can add value which confirms a 0 to 1 Array resize...

Carl Schreiber
6609
Carl Schreiber 2015.12.11 23:02  
jasonxfield:

Sorry gooly can you give me an example of a comment line to request.. mql4 doc for is limited and states array can not be passed in comment function..? thanks

e.g. before the last line:

Comment(__LINE__,"  sz Opened[0] ",ArrayRange(Opened,0),"  sz Opened[1] ",ArrayRange(Opened,1)); //now you'll see!
Print("Opened[8] = ",Opened[8][1]);

If your code crashes at Print, Comment(..) is the last being executed!

Do it for all your array accesses with the correct array!

Jason Crossfield
155
Jason Crossfield 2015.12.12 04:11  

Thanks again.

 52 sz Opened[0] 0 sz Opened[1] 12

So this suggests the Array is not being filled as opened[0] at the comment line should result 9 - correct ? 

Jason Crossfield
155
Jason Crossfield 2015.12.12 04:11  
correction should result 10 :-)
Jason Crossfield
155
Jason Crossfield 2015.12.12 04:43  

And obviously the array is not being filled as a result of the Array out of range error.  

 

I had simplified my code in the provided example above and in doing so forgot to change the assignment of the second array parameter size.  I have now corrected this as below however my problem still remains with the Array out of range error.

 

string Opened[][2],Closed[][2];
12
To add comments, please log in or register