Board index » delphi » Storing a pointer to a record type in the Tag property

Storing a pointer to a record type in the Tag property

Has anyone done this? Does it make sense?
Please provide code snipets/details.

Another question:
Is it feasible to create a descendant of a TField with additional
properties? How does this affect datasets etc?

Thanks in advance.

--
Bruce Swart (br...@cm.co.za)

 

Re:Storing a pointer to a record type in the Tag property


Quote
Bruce Swart wrote:

> Has anyone done this? Does it make sense?
> Please provide code snipets/details.

if it can be done, then it would look something like this:

type
  PMyRecord = ^TMyRecord;
  TMyRecord = record
    myfield : string;
  end;

procedure TForm1.Test;
var
  myrecord:TMyRecord;
begin
  myrecord.myfield := 'hello world!';
  edit1.tag := integer(@myrecord); {or longint(@myrecord) if using D1}
  edit1.text := PMyRecord(pointer(edit1.tag))^.myfield; {or something}
end;

Quote
> Is it feasible to create a descendant of a TField with additional
> properties? How does this affect datasets etc?

I... I wanted to do something like that one time, but I gave up. I don't
think it's possible, there are too many dependancies (enumerated types
and stuff like that down deep in the VCL source code).

--
Rune

Re:Storing a pointer to a record type in the Tag property


Bruce Swart says...

Quote
> Has anyone done this? Does it make sense?
> Please provide code snipets/details.

Well... don't know why you'd want to fo this, but the
following would work (might be a bit convoluted, but...)
Hope it helps.
Mark

TLong = record
case Word of
  0: (Ptr: pointer);
  1: (Long: Longint);
end;

PRec = ^TRec;
TRec = record
  AString: string;
end;

then... save it
procedure
var
  Rec: PRec;
  L: TLong;
begin
  {store it}
  New(Rec);
  Rec^.AString := 'Testing a record pointer';
  L.Ptr := pointer(Rec);
  AComponent.Tag := L.Long;
  ...

then... access it
procedure
var
  Rec: PRec;
  L: TLong;
begin
  L.Long := AComponent.Tag;
  Rec := L.Ptr;
  Label3.Caption := MyRec^.AString; //test
  ...

Re:Storing a pointer to a record type in the Tag property


In article <MPG.fe1b4aaa66eea55989...@news.mr.net>, mdr...@norshor.dst.mn.us

Quote
(Mark Rafn) writes:
>Well... don't know why you'd want to fo this, but the
>following would work (might be a bit convoluted, but...)
>Hope it helps.
>Mark

>TLong = record
>case Word of
>  0: (Ptr: pointer);
>  1: (Long: Longint);
>end;

>PRec = ^TRec;
>TRec = record
>  AString: string;
>end;

>then... save it
>procedure
>var
>  Rec: PRec;
>  L: TLong;
>begin
>  {store it}
>  New(Rec);
>  Rec^.AString := 'Testing a record pointer';
>  L.Ptr := pointer(Rec);
>  AComponent.Tag := L.Long;
>  ...

>then... access it
>procedure
>var
>  Rec: PRec;
>  L: TLong;
>begin
>  L.Long := AComponent.Tag;
>  Rec := L.Ptr;
>  Label3.Caption := MyRec^.AString; //test
>  ...

That's a long way round - any 32-bit value can be stored in the tag, but you
have to type-cast it going in and coming out, because the compiler thinks it
stores only longints.

MyComponent.Tag := longint(PMyRecord);

MyData := PMyRecord(MyComponent.Tag)^.MyRecordField;

Alan Lloyd
alangll...@aol.com

Other Threads