Question about ArrayResize()

 

In the meta editor help description of ArrayResize() it says this:

Note: Array declared at a local level in a function and resized will remain unchanged after the function has completed its operation. After the function has been recalled, such array will have a size differing from the declared one.

Does this mean I cannot use ArrayResize() to resize an array declared in the start() function and I should therefore declare the array globaly ?

 

In my experience ...

A) you can use ArrayResize() in start()

B) there is no need to declare such arrays globally

If the size for an array is always going to be the same, then either:
- consider making it a global & set size in init(), or
- maybe just resize once in start (using perhaps something like 'static bool FirstTimeOnly=true' )

I actually use arrays a lot, as a 'pseudo-OOP' technique, where the object's attributes are stored in an array that is passed around.

 

ok thanks brewmanz I wasnt quite sure what they meant by that in the documentation maybe its outdated info

 

I think what the doc was trying to say that if you declare TYPE name[5] and then call array resize, the next call the size will not be 5 but whatever it was previously resized to.


Also FWIW, it's not necessary to copy array elements

bool    ResizeBuffer(double& buffer[], int size){
    if (ArraySize(buffer) != size){
        ArraySetAsSeries(buffer, false);    // Shift values B[2]=B[1]; B[1]=B[0]
        if (ArrayResize(buffer, size) <= 0){
            trading.disabled    = "ArrayResize [1] failed: "+GetLastError()
                                + " Trading disabled.";
            Alert(trading.disabled);    Print(trading.disabled);
            return(false);  }
        ArraySetAsSeries(buffer, true);
    }
    return(true);
}
This is part of my "EA equivalent of indicator buffers" code, attached.
Files:
polyline.mq4  6 kb
Reason: