Download MetaTrader 5
To add comments, please log in or register
How to test a program before buying?
parham.trader
193
parham.trader 2014.04.04 18:48 

Hi my friends,

I don't know how to deal with this problem.

I've written a code by use of Arrays but I don't know why it doesn't work or compile on the graphs. and the most incredible thing is that I was using some other codes look like this but now it doesn't work !!

it print in journal " array out of range in test.mql4 " .

Here is my test code :

//+------------------------------------------------------------------+
//|                                                         test.mq4 |
//|                                                    Parham.trader |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Parham.trader"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

int i,j,array[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
//u=test_func(i);
   for(i=0;i<4;i++)
     {
      array[i]=i+1;
      j=array[i];
     }
   Alert(j);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
whroeder1
13597
whroeder1 2014.04.04 19:21  
parham.trader: I've written a code by use of Arrays but I don't know why it doesn't work or compile on the graphs. and the most incredible thing is that I was using some other codes look like this but now it doesn't work !! it print in journal " array out of range in test.mql4 " .
int i,j,array[];

Array has no size and you don't resize it.
parham.trader
193
parham.trader 2014.04.05 10:41  
WHRoeder:
Array has no size and you don't resize it.

Isn't it possible to use a dynamic array in cases like this?
Keith Watford
Moderator
7875
Keith Watford 2014.04.05 11:43  
parham.trader:

Isn't it possible to use a dynamic array in cases like this?

Yes, but when assigning a variable to the array index position, you have to make sure that the index position exists
parham.trader
193
parham.trader 2014.04.05 11:55  
GumRai:

Yes, but when assigning a variable to the array index position, you have to make sure that the index position exists


thank you for your response, So I understood from the answers that the best way for assigning the array's index is to make an array in size of 1 ( for example a[1]) and when I want to add any other index then I resize it (ArrayResize(a,2) ) and add a new array to it also add my variable to the newly added index for example. ( a[1]=1 ).

Am I correct?

Keith Watford
Moderator
7875
Keith Watford 2014.04.05 12:37  
parham.trader:


thank you for your response, So I understood from the answers that the best way for assigning the array's index is to make an array in size of 1 ( for example a[1]) and when I want to add any other index then I resize it (ArrayResize(a,2) ) and add a new array to it also add my variable to the newly added index for example. ( a[1]=1 ).

Am I correct?


I'm no expert on arrays and I don't understand everything.

But for a dynamic array, declare it as a[] and then resize it to 1

SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.04.05 13:30  
string arrayid[]={"arraynum0","arraynum1","arraynum2"};

int arrayidcnt=ArraySize(arrayid);
/
To add comments, please log in or register