Solved

Dynamic creation of dynamic arrays

Posted on 1998-10-05
3
421 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month10 days, 1 hour left to enroll

624 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