Solved

dynamic array

Posted on 2002-04-27
10
323 Views
Last Modified: 2010-05-18
How can I create a dynamic array (from variant ?) of a user defined record ?

Andrew
0
Comment
Question by:andrewyu
10 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6972947
type
  TRecord = record
              ....
            end;
  TRecArray : Array of TRecord;

var
  ARecArray : TRecArray;

...

     
0
 

Author Comment

by:andrewyu
ID: 6972949
How can I resize or define dimension of ARecArray ?

Andrew
0
 
LVL 22

Expert Comment

by:mnasman
ID: 6972991
 SetLength(MyArr,100);
0
 

Author Comment

by:andrewyu
ID: 6973396
Therefore, I can access the array from MyArr[0] to MyArr[99].

However, how can I create a multi-dimension array with SetLength ?

Andrew
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 24

Accepted Solution

by:
Tomas Helgi Johannsson earned 100 total points
ID: 6973488
   Hi!

Add the following to the type declartion :
     TMRecArray = Array of TRecArray;

The variable should be
ARecArray : TMRecArray;

Then to initialize the multidimenisionalarray
like this

procedure TForm1.FormCreate(Sender: TObject);
var
   i,j,max : Integer;
begin
     max := 100
     SetLength(ARecArray,max);
     for j := 0 to max-1 do
     begin
          SetLength(ARecArray[j],max);
     end;
     for i := 0 to max-1 do
         for j := 0 to max-1 do
           ARecArray[i][j].<record field name> := <what ever>;
end;

This example is a 100x100 matrix. If you want to add a dimmension to your array then you just add in the type declaration <a new name of array type> : <the last name of array type> and so on. And the variable to the new name of the array type.
Then in the initalization you should add some code like this

procedure TForm1.FormCreate(Sender: TObject);
var
   i,j...,z,max : Integer;
begin
     max := 100
     SetLength(ARecArray,max);
     for j := 0 to max-1 do
     begin
          SetLength(ARecArray[j],max);
          for k := 0 to max-1 do
          begin
              SetLength(ARecArray[j][k],max);
              for ...
              begin
                  SetLength(ARecArray[j][k]....,max);
              end
          end;
     end;
end;
 
The for loops for each additional dimmension should be like the for k:= - loop.

Just remember to free the memory :)

Regards,
Tomas Helgi
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6973847
Have you been helped here, or is more needed?
Moondancer - EE Moderator
0
 

Author Comment

by:andrewyu
ID: 6974397
Thank you very much !

Andrew
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6974821
Thank you, Andrew, for returning and finalizing this.
Moondancer - EE Moderator
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

705 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

18 Experts available now in Live!

Get 1:1 Help Now