BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

how to pass a record to a component?

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Language ObjectPascal
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Thu Mar 08, 2007 10:31 pm    Post subject: how to pass a record to a component? Reply with quote



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;
Back to top
ils
Guest





PostPosted: Mon Mar 12, 2007 3:50 pm    Post subject: Re: how to pass a record to a component? Reply with quote



On 8 อมา, 19:31, Poetj...@gmail.com wrote:
Quote:
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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Language ObjectPascal All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.