3

I've created a record type TTableData in Pascal for storing information from a TStringGrid for later use:

TTableData = record
  header: String[25];   //the header of the column (row 0)
  value : String[25];   //the string value of the data
  number: Integer;      //the y-pos of the data in the table
end;

But whenever I try to initialize these objects by traversing through the TStringGrid and obtaining values from cells the values become ('','',0) (With the exception of a few cells whhich somehow turn out alright).

Here's my procedure for reading in the data from the TStringGrid:

procedure TfrmImportData.butDoneClick(Sender: TObject);
begin
  Halt;
end;

{ initialize records which are responsible
for storing all information in the table itself }
procedure TfrmImportData.initTableDataObjects();

var
  i, j: Integer;

begin
  SetLength(tableData, StringGrid1.ColCount, StringGrid1.RowCount);

  for j:= 0 to StringGrid1.RowCount-1 do begin
    for i:= 0 to StringGrid1.ColCount-1 do begin
      with tableData[i,j] do begin
        header := StringGrid1.Cells[i,0];
        value := StringGrid1.Cells[i,j];
        number := i;
      end;
    end;
  end;

  for i:= 0 to StringGrid1.RowCount - 1 do begin
    for j:=0 to StringGrid1.ColCount - 1 do begin
        ShowMessage(tableData[i,j].header+': '+tableData[i,j].value);
    end;
  end;
end;

I'm not really sure whats happening here. When I use breakpoints and traverse through the code slowly, I can see that the data is initially being read in correctly (through holding the mouse over the tableData[i,j] from the second for-loop to see its current value) but when I try to ShowMessage(...) in the loop itself the value comes out wrong.

Thanks in advance,

Ivan Barayev
  • 2,035
  • 5
  • 24
  • 30
  • 2
    In the first loop, you use `tableData[Col,Row]` and in the second loop you use `tableData[Row,Col]` – Jørn E. Angeltveit May 02 '11 at 21:19
  • As others have said, you're switching Col/Row in your two loops. Just as a note: If you'd change your loop variables from i and j to r and c, where r represents row and c represents col, you wouldn't have this mixup. :) – Ken White May 02 '11 at 21:41
  • 2
    @Ken White: or, bob help us, "row" and "col", or even "column". Now that we're not limited to 69 columns per line and 6 characters per variable name it's possible to have quite descriptive names and readable code. –  May 03 '11 at 04:54

2 Answers2

1

When assigning you are addressing the cells[Col, Row], which is correct. In your control loop (ShowMessage) you have switched to addressing [Row, Col], which is incorrect.

MGH
  • 1,189
  • 1
  • 10
  • 18
0

You're mixing row/col and i/j in your code.

This is probably what you intend to do:

procedure TfrmImportData.initTableDataObjects();
var
  i, j: Integer;

begin
  SetLength(tableData, StringGrid1.RowCount, StringGrid1.ColCount);

  for i:= 0 to StringGrid1.RowCount-1 do begin
    for j:= 0 to StringGrid1.ColCount-1 do begin
      with tableData[i,j] do begin
        header := StringGrid1.Cells[i,0];
        value := StringGrid1.Cells[i,j];
        number := i;
      end;
    end;
  end;

  for i:= 0 to StringGrid1.RowCount - 1 do begin
    for j:=0 to StringGrid1.ColCount - 1 do begin
        ShowMessage(tableData[i,j].header+': '+tableData[i,j].value);
    end;
  end;
end;
Wouter van Nifterick
  • 23,603
  • 7
  • 78
  • 122
  • 2
    You are mixing row/col too. In a StringGrid.Cells the first index is col, the second is row. Very confusing indeed. – kludg May 02 '11 at 19:06
  • @Serg: A little confusing to get used to, but perfectly logical. It's the same system (horizontal, vertical) that Windows graphics functions use for drawing. For instance, LineTo(0, 0, 150, 100) means to draw a line from horiz position 0, vertical position 0 to horizontal position 150, vertical position 100, or in pixel arrays, the col/row of 0, 0 to the col/row of 150/100. – Ken White May 02 '11 at 21:45