Jump to content
Ian Branch

Array within an array??

Recommended Posts

Hi Team,

Need some help/guidance.

I have an array of labels 12 x 7.  These I can address by something like 'Matrix[0,1] to Matrix[12,7]'.

Each label will have an integer value, and colour integer associated with it.  e.g.  [124, 255] the 124 in this case to be converted to string for the label's caption.

How do I define an array, possible an array within a matrix, to accommodate this?

Having defined the array how do I address an individual item in one of the label's array?

 

Regards & TIA,

Ian

Share this post


Link to post

It's just a multidimensional array of record. 

 

You will need to decide whether it is zero based or one based, but your example above shows a range of at least 13 in the major axis. 

Share this post


Link to post

Hi David,

I figured it was going to be a multidimensional array of record.

So I tried the following..

type
  TForm26 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    type
    TLabelRecord = record
      iValue: Integer;
      iColor: Integer;
    end;
    LabelMatrix = array of array of TLabelRecord;

  public
    { Public declarations }
  end;

var
  Form26: TForm26;

implementation

{$R *.dfm}

procedure TForm26.Button1Click(Sender: TObject);
begin
SetLength(LabelMatrix, 11, 6);


end;

Delphi tells me the SetLength code in the Button Click is wrong, object (LabelMatrix) cannot be passed as a parameter, but I don't know why.  😞

I don't care if it starts at 0 or 1, whatever the default is.

I don't know what to do from here.. 😞

Share this post


Link to post

Hi David,

OK.  I think I understand.  So now I have..

type
  TForm26 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    type
      TLabelRecord = record
        iValue: Integer;
        iColor: Integer;
      end;

    type
      TLabelMatrix = array[1..12, 1..7] of TLabelRecord;

  public
    { Public declarations }
  end;

var
  Form26: TForm26;

implementation

{$R *.dfm}

procedure TForm26.Button1Click(Sender: TObject);
var
  rRec: TLabelRecord;
  mLabelMatrix: TLabelMatrix;
begin



end;

But how do I address one element of rRec within one element of mLabelMatrix?

Share this post


Link to post

I surrender.

I have gone to two arrays..

    LabelValue: array of array of Integer;
    LabelColor: array of array of Integer;

Thank you for your suggestions/advice.

 

Ian

Share this post


Link to post
15 minutes ago, Ian Branch said:

I surrender.

I have gone to two arrays..


    LabelValue: array of array of Integer;
    LabelColor: array of array of Integer;

Thank you for your suggestions/advice.

 

Ian

That's odd because you already solved the problem properly. Why do you just give up?

Share this post


Link to post
55 minutes ago, Ian Branch said:

But how do I address one element of rRec within one element of mLabelMatrix?

mLabelMatrix[i, j]

or if you prefer

mLabelMatrix[i][j]

as documented in the link I gave you

Edited by David Heffernan

Share this post


Link to post

Because I wasn't seeing it and getting more and more confused so I decided to take the easy way out.

I put it down, had a coffee, and came back to it.

I now have it..

type
  TForm26 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    type
      TLabelRecord = record
        iValue: Integer;
        iColor: Integer;
      end;

    type
      TLabelMatrix = array[1..12, 1..7] of TLabelRecord;

  public
    { Public declarations }
  end;

var
  Form26: TForm26;

implementation

{$R *.dfm}

procedure TForm26.Button1Click(Sender: TObject);
var
  LabelRecord1, LabelRecord2: TLabelRecord;
  LabelMatrix: TLabelMatrix;
begin
  LabelRecord1.iValue:= 123;
  LabelRecord1.iColor := 234;
  LabelMatrix[1,1] := LabelRecord1;
  //
  LabelRecord2 := LabelMatrix[1,1];
  ShowMessage('Value = '+LabelRecord2.iValue.ToString);
  ShowMessage('Color = '+LabelRecord2.iColor.ToString);
  //
end;

Thank you for your patience & guidance.

 

Ian

  • Like 2

Share this post


Link to post
4 hours ago, Ian Branch said:

I put it down, had a coffee, and came back to it.

 

 

Sometimes all it takes is to walk away from the problem for a short while.

  • Like 1

Share this post


Link to post

A warning: Using records instead of objects in containers carries the penalty of duplication.

  LabelRecord1.iValue:= 123;
  LabelRecord1.iColor := 234;
  LabelMatrix[1,1] := LabelRecord1;
  LabelRecord1.iColor := 567;

At this point, LabekMatrix{1,1].iColor will still be 234.
It is the same the other way around.  Modify the array value, and the variable stays unchanged.

A workaround would be to use pointers.
type PLabelRecord = ^TLabelRecord;
and use variables and arrays of that type.

You would need to New/Dispose each reference, but at least there is no duplication as you pass around the pointer reference to the original allocation instead of copying the record like in the original example.  

Share this post


Link to post
1 hour ago, Lars Fosdal said:

A workaround would be to use pointers.
type PLabelRecord = ^TLabelRecord;
and use variables and arrays of that type.

Pretty bad idea this. It's also attempting to solve a problem that doesn't exist. 

 

Like if I do this:

 

i := 12;
j := i;
i := 14;

 

I don't expect j to become 14. It's just value type semantics.

Share this post


Link to post

True, it is only a problem if you pass the values around to be modified.

Share this post


Link to post
10 minutes ago, Lars Fosdal said:

True, it is only a problem if you pass the values around to be modified.

Same for integers, right? We don't feel compelled to store pointers to integers in arrays very commonly, do we. 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×