Why does this script produce a out of range ?

 

Hello,

Why does this script produce a out of range ?

Thanks,
Pierre

//+------------------------------------------------------------------+
//|                                                 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                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---

   int array[];

   array[0]=1;

   Print("array[0] :"+IntegerToString(array[0]));

  }
//+------------------------------------------------------------------+
 
   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].

@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

 

Hello,

Is it possible to handle exceptions with MQL5 like a out of range error?

Thanks,
Pierre8r

 

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.
Reason: