but at the beginning of developing a new idea,
I'm always make a for loop first,
my problem happens on code below, please help!
function dothings( k1234z )
{
ret = 0; // result
barNo_case1 = 0; v_case1 = 0;
barNo_case2 = 0; v_case2 = 0;
barNo_case3 = 0; v_case3 = 0;
for( i = BarCount; i >= 0; i-- )
{
if ( k1234z[i] == 1 ) { barNo_case1 = i; v_case1 = O[i]; }
// Error 10. Array subscript out of range. You must not access array elements outside 0..(BarCount-1) range. You attempted to access non-existing 200-th element of array..
if ( k1234z[i] == 2 ) { barNo_case2 = i; v_case2 = O[i]; }
// Error 10. Array subscript out of range. You must not access array elements outside 0..(BarCount-1) range. You attempted to access non-existing 200-th element of array..
// ...
}
// base on barNo_case* and v_case* above to do something much complex
// finally generate result array named 'ret' ...
return ret; // result
}
k1234z = IIf(O<C, 1, IIf(O==C,2, 3)); // create some cases
k1234z = dothings( k1234z ); // do something complex base on cases above
Yes!
I need to check values from the right-side to the left (from BarCount-1 to 0).
For example,
after recording values on the right-side (ex. bar 1000=a, bar 900=b, bar 800=c),
then I can decide some values (ex. 5 values),
when this loop go to the left-side (ex. bar 700).
Such requirement seems complex...
Normally, single line array processing statement can assign one value only,
for example, barNo_case1 = ValueWhen(k1234z == 1, BarIndex());
but I need to record some values on the right-side first,
then I can assign some other values (ex. 5 values) when it matches my case,
so I try to deal it with a reversed for loop.
Array subscript out of range. You must not access array elements outside 0..(BarCount-1) range. You attempted to access non-existing 200-th element of array
If an array has 5 elements. And you write for( i = 5; i >= 0; i-- ). It means that you are asking the compiler to loop:
1st Loop i = 5
2nd Loop i = 4 (since you're using i--)
3rd Loop i = 3
4th Loop i = 2
5th Loop i = 1
6th Loop i = 0 (since you wrote i>= 0)
But the array has 5 elements only, compiler will throw an error right away as you are trying to access something that does not exist.
Now if you write for( i = 4; i >= 0; i-- ). It means you are asking the compiler to loop and it will work fine without an error (obviuosly):
1st Loop i = 4
2nd Loop i = 3
3rd Loop i = 2
4th Loop i = 1
5th Loop i = 0
That's why for( i = BarCount - 1; i >= 0; i-- ) does not throw an error. You have set 200 bars (0 to 199) as your preference:
So, when you write i = BarCount it access the i = 200th bar which obviously does not exist as the count starts from 0.