Array not filling with value

 

Greetings, 

I've recently started learning MQL, and I have stumbled upon this issue. I'm looping the total num of bars and trying to assign a value to an array. The problem is that the array seems to remain empty even though I assign a value to it.  Here's an example: 

//+------------------------------------------------------------------+
//|                                               Demo Indicator.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""
#property version   "1.00"
#property strict
#property indicator_separate_window

double testVar[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  { 
   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[])
  {
//---
    int total = 0;
    if(prev_calculated == 0)
     {
      // first time calc
      total = rates_total -1;
     }
   else
     {
      // second interaction on
      total = rates_total - prev_calculated;
     }

     
     for(int i=0;i<total;i++)
       {
        testVar[i] = 1;
       }
       
       Comment("The value of first element is: ", testVar[0]);
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

I'm expecting the Comment at the end to say "The value of the first element is: 1", but instead the comment never shows. If I instead remove testVar from the comment then I can see it working. It is simply not assigning a value to this array.  What am I doing wrong ?

 
Nightcrawler:   What am I doing wrong ?
  1. Look in the log. You will see that your code crashed and why.
  2. Next time, use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?

 
William Roeder:
  1. Look in the log. You will see that your code crashed and why.
  2. Next time, use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?

Thank you for your fast reply and thank you very much for pointing out the debugger, I was unware of its existance. I've tried to use it but all I can see is the journal saying "loaded successfully" then "removed", with no messages whatsoever in the debug tab.  I've also tried to "Comment()" the value of _LastError, but nothing comes out. I guess for now I'll just try to learn more about the debugger. Thanks!
 
  1. Look in the Experts tab.
  2. Set breakpoints before running the debugger.
 
William Roeder:
  1. Look in the Experts tab.
  2. Set breakpoints before running the debugger.
Thank you once again, I was able to get the error. There is an array out of range error. I've found endless material on different reasons why this error is showing, I'm only to figure out why in my code. Thanks for these useful pointers!
 

In case anybody struggles, I was able to fix my "out of range" error by simply setting getting the bars count and resizing the array to this value:

int IBARS = iBars(Symbol(), Period());
if(ArraySize(testVar) < IBARS) {
    ArrayResize(testVar, IBARS);
}
Reason: