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

 

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. 

 
    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++;!!
 
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... ?  
 
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!
 

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.

 

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

 

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

 
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!

 

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 ? 

 
correction should result 10 :-)
 

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];
Reason: