Solved

Dynamic creation of dynamic arrays

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 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

12 Experts available now in Live!

Get 1:1 Help Now