Solved

Dynamic creation of dynamic arrays

Posted on 1998-10-05
3
409 Views
Last Modified: 2010-04-06
I have a 4 dimensional dynamic array. At run time I read a file to determine how many of these arrays I need and what their names should be.

How can I create one or more of these dynamic arrays and assign their name at run time so that I may then call them as and when by their name to assign values to certain elements?
0
Comment
Question by:leeberry2
3 Comments
 

Author Comment

by:leeberry2
ID: 1341792
The points are high not because of difficulty but because i'm after a speedy reply.
0
 
LVL 4

Accepted Solution

by:
BoRiS earned 300 total points
ID: 1341793
leeberry2

here is a code snippet I recieved once...

Below is an example of a dynamic array:



}



unit Unit1;



interface



uses

  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics,

  Controls, Forms, Dialogs, StdCtrls;



type

  ElementType = Integer;



const

  MaxArraySize = (65520 div SizeOf(ElementType));

    { in a 16-bit environment }



type

  { Create the array type.  Make sure that you set the range to

    be the largest number you would possibly need. }

  TDynamicArray = array[1..MaxArraySize] of ElementType;

  TForm1 = class(TForm)

    Button1: TButton;

    procedure FormCreate(Sender: TObject);

    procedure Button1Click(Sender: TObject);

    procedure FormDestroy(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;



var

  Form1: TForm1;

  { Create a variable of type pointer to your array type. }

  P: ^TDynamicArray;



const

  { This is a typed constant.  They are actually static

    variables hat are initialized at runtime to the value taken

    from the source code.  This means that you can use a typed

    constant just like you would use any other variable.  Plus

    you get the added bonus of being able to automatically

    initialize it's value. }

  DynamicArraySizeNeeded: Integer = 10;



implementation



{$R *.DFM}



procedure TForm1.FormCreate(Sender: TObject);

begin

  { Allocate memory for your array.  Be very careful that you

    allocate the amount that you need.  If you try to write

    beyond the amount that you've allocated, the compiler will

    let you do it.  You'll just get data corruption. }

  DynamicArraySizeNeeded := 500;

  P := AllocMem(DynamicArraySizeNeeded * SizeOf(Integer));

  { How to assign a value to the fifth member of the array. }

  P^[5] := 68;

end;



procedure TForm1.Button1Click(Sender: TObject);

begin

  { Displaying the data. }

  Button1.Caption := IntToStr(P^[5]);

end;



procedure TForm1.FormDestroy(Sender: TObject);

begin

  { Free the memory you allocated for the array. }

  FreeMem(P, DynamicArraySizeNeeded * SizeOf(Integer));

end;



end.

Hope this helps

Later
BoRiS
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1341794
You mean Delphi4's dynamic arrays?

You could use a 5 dimensional array...

var AllYourDynamicArrays : array of record
                                      arrName : string;
                                      arr     : TYour4DimArrayType;
                                    end;

The problem is: you can't call them directly by name. You'll have to do someting like that:

function Get4DArray(name: string) : integer;
var i1,i2 : integer;
begin
  result:=-1;
  i2:=high(AllYourDynamicArrays);
  for i1:=0 to i2 do
    if AllYourDynamicArrays[i1]=name then begin
      result:=i1;
      break;
    end;
end;

procedure SetValue(name: string; value: TBlaBla);
var i1 : integer;
begin
  i1:=Get4DArray(name);
  if i1>-1 then AllYourDynamicArrays[i1].arr[dim1][dim2][dim3][dim4].value:=value;
end;

Regards, Madshi.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question