In article <MPG.14812d338af2df84989...@news.nzl.ihugultra.co.nz>,
Peter Huebner <peterh@this_bounces.igrin.co.nz> wrote:
Quote
> I have very little experience of using pointers in Delphi.
> I tried the other day to pass a pointer to an element in
> a dynamic array of MyRecordType to a procedure, then
> change the values of some of the fields in the procedure.
> I got absolutely nowhere - the code I wrote compiled just
> fine, but the values did not appear in the array after
> the procedure had executed.
> 1. attempt:
> -----------
> procedure TLedger.PokeSomeValues(AnItem: ^MyRecordType);
> begin
> AnItem^.account := somestring;
> AnItem^.flag1 := true;
> AnItem^.flag2 := false;
> AnItem^percent := 55;
> end;
> When I did a break all the assigned values were empty.
Is this exactly the code you used? It doesn't even compile
for me - the error is at the ^MyRecordType:
"Type identifier expected, ^ found".
If I fix that it works just fine - maybe the problem is
in the syntax of the call or something. Try this:
type
PMyRecordType = ^TMyRecordType;
TMyRecordType = record
n, m: integer;
end;
procedure InsertAnswer(ARecord: PMyRecordType);
begin
ARecord^.n:= 42;
ARecord^.m:= 42;
end;
procedure TForm1.Button1Click(Sender: TObject);
var r: TMyRecordType;
begin
InsertAnswer(@r);
ShowMessage(inttostr(r.n) + ' = ' + inttostr(r.m));
end;
It works for me. You can leave out some of the ^'s, btw.
If you have an actual reason for using pointers here you
must be allocating memory. You'd call InsertAnswer with
a dynamically-allocated TMyRecordType like so:
procedure TForm1.Button2Click(Sender: TObject);
var p: PMyRecordType;
begin
New(p);
InsertAnswer(p);
ShowMessage(inttostr(p.n) + ' = ' + inttostr(p.m));
Dispose(p);
end;
That works for me too.
But I don't think there's really any reason to use pointers
in the procedure. Instead I'd say
procedure VarInsertAnswer(var ARecord: TMyRecordType);
begin
ARecord.n:= 42;
ARecord.m:= 42;
end;
and I'd call that as in either
procedure TForm1.Button3Click(Sender: TObject);
var r: TMyRecordType;
begin
VarInsertAnswer(r);
ShowMessage(inttostr(r.n) + ' = ' + inttostr(r.m));
end;
or
procedure TForm1.Button4Click(Sender: TObject);
var p: PMyRecordType;
begin
New(p);
VarInsertAnswer(p^);
ShowMessage(inttostr(p.n) + ' = ' + inttostr(p.m));
Dispose(p);
end;
Not sure what the problem in your code is - what you posted
didn't compile for me, and when I fixed that what I got made
me think the problem could be in the parts you're not
showing us. But everything above works.
--
Oh, dejanews lets you add a sig - that's useful...
Sent via Deja.com http://www.deja.com/
Before you buy.