Solved

Dynamic creation of dynamic arrays

Posted on 1998-10-05
3
403 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Delphi XE10 Round Image 2 154
control image tags in a string ? 12 140
Microsoft Access 97 and Delphi XE2 9 50
How to insert .png images to TClientDataSet .cds file? 2 30
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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

792 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