Why am I getting "Array out of range" error?

 
I'm still trying to wrap my head around creating and accessing arrays... Not quite sure what I did wrong here.
Thanks for any insights. :)

 
      double SwingValue[];
      int sBars;
          sBars=Bars;
      
      for( int i = sBars; i>=0; i--)       //starts at first bar, to current bar
      {
            SwingValue[i]=iCustom(Symbol(),_Period,"ZigZag",ExtDepth,ExtDeviation,ExtBackstep,0,i);
            if(SwingValue[i] !=0)
            {
            Print(SwingValue[i]);
            }
      }

 
After declaration, You need to resize array to Bars amount of items.
 
Fabio Cavalloni #:
After declaration, You need to resize array to Bars amount of items.

Thanks :) but even with this in here, I am still getting Array out of range for the "SwingValue[i]=iCustom(Symbol(),_Period,"ZigZag",ExtDepth,ExtDeviation,ExtBackstep,0,i);" line 9which is where the error was initially as well.
I've tried it at multiple locations, this one seems to make the most sense. sBars or Bars does not seem to matter.

      int sBars;
          sBars=Bars;

      double SwingValue[];                                      //---Moved below int sBar
      ArrayResize(SwingValue,sBars);                    //--------Added Line
         
      for( int i = sBars; i>=0; i--)       //starts at first bar, to current bar
      {
            SwingValue[i]=iCustom(Symbol(),_Period,"ZigZag",ExtDepth,ExtDeviation,ExtBackstep,0,i);
            if(SwingValue[i] !=0)
            {
            Print(SwingValue[i]);
            }
      }


[EDIT]

Ive changed the 

for( int i = sBars; i>=0; i--)

to 

for( int i = sBars-1; i>=0; i--)


which makes sense I guess.

code seems to be working now... let's see if my changes have any influence further down the script.
Thanks :)
 
for( int i = sBars; i>=0; i--) 

should be

for( int i = sBars-1; i>=0; i--) 
 
Keith Watford #:

should be

:) Thanks Keith.

I think I realised that just as I was trying to get stuff to work.
Glad I got to the right conclusion. Tells me I'm learning.
Reason: