• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 336
  • Last Modified:

dynamic array

How can I create a dynamic array (from variant ?) of a user defined record ?

Andrew
0
andrewyu
Asked:
andrewyu
1 Solution
 
kretzschmarCommented:
type
  TRecord = record
              ....
            end;
  TRecArray : Array of TRecord;

var
  ARecArray : TRecArray;

...

     
0
 
andrewyuAuthor Commented:
How can I resize or define dimension of ARecArray ?

Andrew
0
 
Mohammed NasmanSoftware DeveloperCommented:
 SetLength(MyArr,100);
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
andrewyuAuthor Commented:
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
 
Tomas Helgi JohannssonCommented:
   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
 
MoondancerCommented:
Have you been helped here, or is more needed?
Moondancer - EE Moderator
0
 
andrewyuAuthor Commented:
Thank you very much !

Andrew
0
 
MoondancerCommented:
Thank you, Andrew, for returning and finalizing this.
Moondancer - EE Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now