int array[]; array[0]=1;The array has no size, therefor there is no array[0].
whroeder1:
The array has no size, therefor there is no array[0].
The array has no size, therefor there is no array[0].
@whroeder1's is correct.
Based upon your prior post, perhaps you are confusing how indicators handle buffers (arrays to hold indicator values) and general arrays that you create for your own purposes.
Indicators handle the sizing of arrays (buffers) automatically.
In Scripts or EA's, you must set the dimension of the array yourself.
This can be a fixed amount
int array[4];
or you can use ArrayResize() to set the amount dynamically.
Thanks,
Pierre
Check the size using ArraySize() Before accessing or adding.
But honestly you really need to refactor your code if you need to handle out of range errors. Proper code does not produce these errors.
//+------------------------------------------------------------------+ //| stOutOfRange.mq5 | //| Copyright 2018, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2018, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ int array[]; void OnStart() { //--- ArrayResize(array, 1); array[0]=1; ArrayResize(array, 2); array[1]=2; Print("array[0] :"+IntegerToString(array[0])); Print("array[1] :"+IntegerToString(array[1])); ArrayFree(array); } //+------------------------------------------------------------------+ArrayResize
Array Functions
Thanks.
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hello,
Why does this script produce a out of range ?
Thanks,
Pierre