Solved

Dynamic creation of dynamic arrays

Posted on 1998-10-05
3
395 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now