I am trying to play with the storage of the MQL 5 Array. When I am not using any ArrayResize()
, I am getting error:
double d [];
d[0] = 1;
for (int i = 0; i< ArraySize(d); i++)
{
Print(d[i]);
The error is as follows:
2018.03.26 13:17:25.379 2018.02.02 00:00:00 array out of range in 'testing.mq5' (69,2)
Whereas when I use ArrayResize()
I get output.
double d [];
ArrayResize(d,2);
d[0] = 1;
for (int i = 0; i< ArraySize(d); i++)
{
Print(d[i]);
}
Output: 1
It worked. But, if I try to add the array element beyond the array size then I get out of range
error.
I am will to achieve is that the array must remain dynamic in size perspective.
Say I the size given is 2
and during my program the array element at 3
needs to be added then the array must accept it.
I cannot use ArrayResize()
as it will wipe out other values, which I don't want to happen.
Kindly, suggest me a middle way out so that I can enter any number of values in an array irrespective of its size.
#include <Arrays\ArrayObj.mqh>
is good for storing objects of any class, ArrayInt
for list of integers, ArrayDouble
for list of doubles and floats. Add as many as you need, whenever you need and you will never have to resize or catch index out of array.
CArrayInt *integers;
OnInit(){integers=new CArrayInt();}
OnDeinit(int reason){delete(integers);}
OnTick(){
integers.Add(0);integers.Add(1);integers.Add(2);
int firstElem=integers.At(0); firstElem=integers[0];
int lastElem=integers.At(integers.Total()-1);
int totalElementsInTheList=integers.Total();
integers.Delete(2); // delete element with index 2. deleting element that match with the object is not supported in basic API
}