🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.delphi.language.objectpascal
2 messages
2 total messages Started by Poetje78 Thu, 08 Mar 2007 16:31
how to pass a record to a component?
#164
Author: Poetje78
Date: Thu, 08 Mar 2007 16:31
47 lines
2220 bytes
hi, i have a question,
if i try to run the following code i get the error "Left side cannot
be assigned to".
i understand that this has something to do with the usage of TypeA.
My question is, how can you pass records to components? because,
apparently, the following approach does not work.

thank you all in advance,

Type TypeA=record
  some_int:integer;
  some_real:real;
  end;

Type TypeB = class(TComponent)
  private
    PrivateA: array of TypeA;
    function GetA(index:integer):TypeA;
    procedure SetA(index:integer;value:TypeA);

  public
    procedure SetLengthA(i:integer);
    property A[i:integer]:TypeA read GetA write SetA;
  end;

procedure TypeB.SetLengthA(i:integer);
begin
  setlength(PrivateA,i);
end;

function TypeB.GetA(index:integer):TypeA;
begin
  result:=PrivateA[index];
end;

procedure TypeB.SetA(index:integer;value:TypeA);
begin
  PrivateA[index]:=value;
end;

procedure TForm1.Button1Click(Sender: TObject);
var B: TypeB;
begin
  B:=TypeB.create(nil);
  B.SetLengthA(3);
  B.A[1].some_int:=5;   //   <----------- this causes the error !
end;
Re: how to pass a record to a component?
#165
Author: ils
Date: Mon, 12 Mar 2007 10:50
55 lines
2742 bytes
On 8 ���, 19:31, Poetj...@gmail.com wrote:
> hi, i have a question,
> if i try to run the following code i get the error "Left side cannot
> be assigned to".
> i understand that this has something to do with the usage of TypeA.
> My question is, how can you pass records to components? because,
> apparently, the following approach does not work.
>
> thank you all in advance,
>
> Type TypeA=record
>   some_int:integer;
>   some_real:real;
>   end;
>
> Type TypeB = class(TComponent)
>   private
>     PrivateA: array of TypeA;
>     function GetA(index:integer):TypeA;
>     procedure SetA(index:integer;value:TypeA);
>
>   public
>     procedure SetLengthA(i:integer);
>     property A[i:integer]:TypeA read GetA write SetA;
>   end;
>
> procedure TypeB.SetLengthA(i:integer);
> begin
>   setlength(PrivateA,i);
> end;
>
> function TypeB.GetA(index:integer):TypeA;
> begin
>   result:=PrivateA[index];
> end;
>
> procedure TypeB.SetA(index:integer;value:TypeA);
> begin
>   PrivateA[index]:=value;
> end;
>
> procedure TForm1.Button1Click(Sender: TObject);
> var B: TypeB;
> begin
>   B:=TypeB.create(nil);
>   B.SetLengthA(3);
>   B.A[1].some_int:=5;   //   <----------- this causes the error !
> end;

1. use pointer to record TTypeA like
type
  PTypeA = ^TTypeA;

2. use "array of PTypeA" instead of "array of TTypeA"
3. don't  forget to allocate memory for all elements of that array
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads