array questions - page 2

 
lippmaje:

You cannot resize an array that was created static. You can only resize a dynamic array. ArrayResize() has a return value, check it to detect errors.

Actually you can

   double staticArray[10];
   
   int res=ArrayResize(staticArray,12);
   
   printf("ArraySize = %i, res= %i",ArraySize(staticArray),res);
2020.05.15 22:09:14.268    340761 EURUSD,H1: ArraySize = 12, res= 12
 
Alain Verleyen:

Actually you can

2020.05.15 22:09:14.268    340761 EURUSD,H1: ArraySize = 12, res= 12

Interesting to know. It's somewhat contradicting the mql reference:

Note

The function can be applied only to dynamic arrays.

 
faustf:

yea  i understund  what you tell but , my case is different i know that  array start the count 0 to end  and i  if i create Array [2] ,resize it,  and  with(WindowFirstVisibleBar() + 5)and  cicle for insert inside of array  with  for(int i=WindowFirstVisibleBar(); i>=0; i--) , i aspect the script runn good , but return the same2020.05.15 23:19:13.116    candle-body-size USDCHF,H1: array out of range in 'candle-body-size.mq4' (60,29)
example if
WindowFirstVisibleBar() is  equal = 100  and i resize in 105 , if  for start  to 0 or 1   for me is no problem i have 5 position plus


Sorry, english is not my first language, so I don't quite get what you don't understand.

Basics are simple:

  • if you use static arrays, you must use constant to declare the size, compiler won't accept variables
  • if you use dynamic arrays, you must make sure that you do correct ArrayResize()
  • see Alain's comment on resizing static arrays - it's undocumented, but it seems to work. Since it's undocumented it might stop working in some future version.
  • In any case, you can use as much array elements as you declared. No more.
  • Again, array elements are indexed from 0 to n-1, where n is number of elements.

Finally:

  • "Array out of range" means you are trying to access array element beyond the array end.
  • When you get this error, you have a bug in your code - you have a wrong idea about the size of the array and you didn't properly resize your array.
  • Use debugger and/or print out variables.
  • Your case is not different. There is no any other "special" situation where you can get "Array out of range".

Useful links:

  • read all about Array functions

  • use ArraySize() function and print out actual array size before you try to access array elements. Make sure that you resized your array correctly.


Array Functions - MQL4 Reference
Array Functions - MQL4 Reference
  • docs.mql4.com
. In a particular case of a one-dimensional array of 50 elements, calling of the first element will appear as array[0], of the last one - as array[49]. What's new in MQL5 Added functions for quick insertion, deletion, copying and expanding array elements. The new ArraySwap() function swaps the contents of two dynamic arrays of the...
 
lippmaje:

Interesting to know. It's somewhat contradicting the mql reference:

Note

The function can be applied only to dynamic arrays.

I know, I can't say it has always be like that, but it is at least for the last 6 years. And it will not change because MT4/mql4 is "frozen".

 

sorry  but i continue to not undesrtund i can create static array   double array[10] or dinamic array  double array[] but  after with dinamic i must resize with arrayresize(array,1000),

now i correct the script with dinamic  array but return me the same problem


//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2018, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+


#property indicator_chart_window
extern int    font_size = 10;
extern color  ColorBull = DodgerBlue;
extern color  ColorBeer = Red;
extern string font_name = "Arial";
double aBodyCount[];
   int res;
//+------------------------------------------------------------------+
int start()
  {
     int VisibleBar=WindowFirstVisibleBar();
    res=ArrayResize(aBodyCount,(VisibleBar+5));
      Print("visiblebar "+VisibleBar+" dimention array "+ArraySize(aBodyCount));
     BodyCount();
   return 0 ;
  }
//+------------------------------------------------------------------+
int deinit()
  {
   ObjectsDeleteAll(0,OBJ_TEXT);
   return(0);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|            BODY COUNT VISIBLE                                    |
//+------------------------------------------------------------------+
void BodyCount()
  {
   for(int i=WindowFirstVisibleBar(); i>=0; i--)
     {
      aBodyCount[i] = ((NormalizeDouble(Open[i],Digits)/Point)-(NormalizeDouble(Close[i],Digits))/Point);
     }
   Alert("Plus ", aBodyCount[i]," pt.");
   return ;

  }
//+------------------------------------------------------------------+

the problem is not resize because the print after resize return me this

2020.05.17 21:50:41.781    candle-body-size USDCHF,H1: visiblebar 653 dimention array 658

now  if i do a for  with WindowFirstVisibleBar and is 653    if  i have array of 658 why go in dimention excide?????

the for do only 653 cicle  and arry is 658 how is possible??

 

i find a problem  is  allert


//Alert("Plus ", aBodyCount[i]," pt.");

Reason: