Board index » delphi » Inconsistent problem with Array dimensions

Inconsistent problem with Array dimensions

I'm working with a 2D array, and am puzzled.  This is bare bone code.
The program accepts a cell address of >10 for the first value (x) 1 out
of ten trys - the errors do not occur with the same nX/nY values
consistantly.

The DO loop version has the same problem - Interestingly enough, it
DECREMENTS nX as it executes, while xY increments as I would expect. (?)

Any insight is appreciated...

Regards
BL

...
  AryRollArray : array[1..10] of array[1..13] of String;
...

nX                : Integer;
nY                : Integer;

// Simple array population example

// Do Loop Version
//for nX := 1 to 13 do
//  for nY := 1 to 10  do
//    AryRollArray[nX][nY] := 'some value...';

//While loop version
nX := 1;
nY := 1;
While nX <=13 do
  begin
    While nY <=10 do
      begin
      AryRollArray[nX][nY] := 'some value...';
      nY := nY + 1;
      end;
    nX := nX + 1;
    nY := 1;
  end;

 

Re:Inconsistent problem with Array dimensions


Quote
"Bob Lehy" <bob...@nospam.hotmail.com> wrote in message

news:MPG.13270a43dd17a5ae989683@news1.on.sympatico.ca...

Quote
> I'm working with a 2D array, and am puzzled.  This is bare bone code.
> The program accepts a cell address of >10 for the first value (x) 1 out
> of ten trys - the errors do not occur with the same nX/nY values
> consistantly.

> The DO loop version has the same problem - Interestingly enough, it
> DECREMENTS nX as it executes, while xY increments as I would expect. (?)

> Any insight is appreciated...

> Regards
> BL

> ...
>   AryRollArray : array[1..10] of array[1..13] of String;
> ...

> nX                : Integer;
> nY                : Integer;

> // Simple array population example

> // Do Loop Version
> file://for nX := 1 to 13 do
> //  for nY := 1 to 10  do
> //    AryRollArray[nX][nY] := 'some value...';

You have your dimensions mixed up, 1-10 rows (not 1 - 13), and 1-13 columns
(not 1-10).

Try

for nX := low (AryRollArray) to high (AryRollArray) do
    for nY := low (AryRollArray [nX]) to high Z(AryRollArray [nX]) do
        // etc.

Using low and high will prevent this kind of problem from ever happening
again, even when the array declaration changes.

- Show quoted text -

Quote

> file://While loop version
> nX := 1;
> nY := 1;
> While nX <=13 do
>   begin
>     While nY <=10 do
>       begin
>       AryRollArray[nX][nY] := 'some value...';
>       nY := nY + 1;
>       end;
>     nX := nX + 1;
>     nY := 1;
>   end;

Other Threads