Search code examples
memorynew-operatordisposepascal

Pascal: Store classes in a 2d array with New and Dispose


I'm trying Pascal, and I've defined my own class, Block. I have an array[1..20, 1..20] of Block called PlayGrid, now I'm trying to fill it with Blocks.

This for loop:

for IterY := 1 to 20 do
    for IterX := 1 to 20 do

      //How do I put a new block instance in the PlayGrid?

      end.
    end.

Would you have to use New and Dispose to do this?

Thanks.


Solution

  • This is what I would do:

    type
      TBlock = class    
         // You class stuff goes here
      end;
    
    var 
      PlayGrid: array[1..20, 1..20] of TBlock;
    
    begin
      for X := 1 to 20 do
        for Y := 1 to 20 do
          PlayGrid[X, Y] := TBlock.Create;
    end.