[fpc-pascal] Coercing record fields together

Ralf Quint freedos.la at gmail.com
Tue Jul 10 06:57:44 CEST 2018


On 7/9/2018 9:22 AM, Ryan Joseph wrote:
> Could I do some RTL magic on a record to loop over its fields and set them to another record by name? In the example below I want to set all the fields in TRecA to matching named fields in TRecB. Just curious if we can do this automatically using RTL.
>
> type
> 	TRecA = record
> 		a: string;
> 		b: string;
> 	end;
>
> type
> 	TRecB = record
> 		a: string;
> 		b: string;
> 		c: integer;
> 		d: integer;
> 	end;
>
>
> for field in recA.GetFields do
> 	recB.GetFieldByName(field.name).value := field.value
If you make sure that any variable of tRecB is declared first, you can use

Var RecB : tRecB;
        RecA : tRedA ABSOLUTE RecB;

Quite obviously, it doesn't work the other way around as tRecB is larger 
than tRecA...

Ralf

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus




More information about the fpc-pascal mailing list