Recent

Author Topic: Array of Pointers -- Assignment  (Read 267 times)

abtaylr

  • Full Member
  • ***
  • Posts: 104
Array of Pointers -- Assignment
« on: October 07, 2019, 05:29:48 pm »
I am having a difficult time making an assignment to an array of pointers. Here is the code at issue:
Code: Pascal  [Select]
  1. type
  2.   PGrid = ^TStringGrid;
  3.  
  4.   TPix = array of PGrid;
  5.  
  6.   TPixArr = Class(TStringGrid)
  7.   private
  8.  
  9.   public
  10.     Pix : TPix;
  11.     procedure Add(idx : integer);
  12.     function Count : integer;
  13.  
  14.   end;
  15. ....
  16. procedure TPixArr.Add(idx : integer);
  17. var
  18.   PG : TStringGrid;
  19.   PPG : PGrid;
  20. begin
  21.   PG := TStringGrid.Create(nil);
  22.   PG.ColCount := 10;
  23.   PG.RowCount := 45;
  24.   PPG := @PG;
  25.   Pix[idx] := PPG; // <======
  26. end;
  27.  

The PGrid is set up in the type area, then in TPixArray.Add(idx) I create a StringGrid PG and set PPG to take an assignment of its address. The next line is the problem. In the array at the index number, I try to assign the pointer, but I get an error every time. I have also tried the routine from the Free Pascal Reference Guide, Types, p.46, where you can use a constructor for an array, but that failed also. I don't see what I am doing wrong and the error message merely refers to the line number without anything else.

Thaddy

  • Hero Member
  • *****
  • Posts: 9293
Re: Array of Pointers -- Assignment
« Reply #1 on: October 07, 2019, 05:54:48 pm »
I don't thing you mean a TStringGrid (which is a visual component). but rather a two dimensional array of string? Or a TStringLIST?
Anyway your code is completely wrong, but plz explain what you want exactly.... You give me almost nothing to work with...
Code: Pascal  [Select]
  1. Type
  2.   TColumns = 0..9;
  3.   TRows  = TColumns;  // a square matrix of 10 x 10
  4.  
  5. var
  6.   Stringmatrix: array[TColumns,TRows] of string;
  7.  
  8. procedure  AddMatrix(const Col:TColumns; const Row:TRows; const S:String;);
  9. begin
  10.   StringMatrix[Col,Row] :=S;
  11. end;
  12.  
  13. begin
  14.    StringMatrix[0,0] := 'test';
  15.    // or
  16.    AddMatrix(0,1,'Test2');
  17.    writeln(StringMatrix[0,0]);
  18.    writeln(StringMatrix[0,1]); // etc...
  19. end.

 Something like that?
« Last Edit: October 07, 2019, 06:13:18 pm by Thaddy »
also related to equus asinus.

howardpc

  • Hero Member
  • *****
  • Posts: 3204
Re: Array of Pointers -- Assignment
« Reply #2 on: October 07, 2019, 06:11:44 pm »
Why do you need a pointer to a TStringGrid?
Classes are already pointers.
Perhaps you want something like this:
Code: Pascal  [Select]
  1. type
  2.  
  3.   TPix = array of TStringGrid;
  4.  
  5.   TPixArr = class(TComponent)
  6.   public
  7.     Pix : TPix;
  8.     procedure Add(idx: Integer);
  9.     constructor Create(AOwner: TComponent); override;
  10.   end;
  11.  
  12. implementation
  13.  
  14. procedure TPixArr.Add(idx: integer);
  15. begin
  16.   if (idx >-1) and (idx < High(Pix)) and not Assigned (Pix[idx]) then
  17.     begin
  18.       Pix[idx] := TStringGrid.Create(Self);
  19.       Pix[idx].ColCount := 10;
  20.       pix[idx].RowCount := 45;
  21.     end;
  22. end;              
  23.  
  24. ...

Thaddy

  • Hero Member
  • *****
  • Posts: 9293
Re: Array of Pointers -- Assignment
« Reply #3 on: October 07, 2019, 06:14:37 pm »
Why do you need a pointer to a TStringGrid?
Classes are already pointers.
That's why I did not understand it...This is a beginner...  Both options are different interpretations of what he means.
« Last Edit: October 07, 2019, 06:18:04 pm by Thaddy »
also related to equus asinus.

abtaylr

  • Full Member
  • ***
  • Posts: 104
Re: Array of Pointers -- Assignment
« Reply #4 on: October 07, 2019, 06:59:44 pm »
I started out with a simple array Pixl : an array[0..9,0..44]; and Pix : an array of an array of Pixl.

I have gone through many iterations including making Pix an array of TStringGrid. I haven't tried using TColumns and TRows.  The first example is very close to my first try using a TStringGrid, but I got errors trying to use Pix[idx] := TStringGrid.Create(nil). This is the reason that I finally went to PGrid = ^TStringGrid.  An array of pointers, even though a class is also just a pointer, seemed to work, partially. The code I use in later parts of the application de-referencing the pointers all appear to work. It's just the initial setup that goes awry.

The reason why I use TStringGrid is because I like using the methods of TStringGrid. This all is going into a form not shown here. On that form I have a string grid showing typical rows and columns, but each column also needs to refer an array of Pixels of 10 X 45. I probably would get to the same end as TStringGrid using the TColumns and TRows.

Most of my experience is in C for many years and then Visual Basic in later years on SQL application programming, however, I am very green on Pascal. The closest reference I can make is a relational database tied together with an index, one table drawing a column of single values and another a grid of values. Maybe I should go back to what I know and try to use TDBGrid, but I think I would end up with the same problem.

abtaylr

  • Full Member
  • ***
  • Posts: 104
[SOLVED]Re: Array of Pointers -- Assignment
« Reply #5 on: October 07, 2019, 10:43:10 pm »
Thank you for your assistance! I finally got my code working using a variant of the HowardPC code. Comments from both of you were helpful.