Variant Records: The Equivalent To The C-Union Structure
Is there a Way to create a c 'union'-like structure in Delphi? That IS, A STRUCTURE THAT Uses The Same Memory Area?
The Delphi (Pascal / ObjectPascal) equivalent to a C-union structure is called a Variant Record (not to be confused with the Variant "type" available in Delphi 2.0 ). As with a C-union, the Pascal variant record allows several structure types to be combined into one, and all will occupy the same memory space Look up the syntax declaration under "Records" in the help file But here's an example: type TPerson = record FirstName, LastName: string [40]; BirthDate.. : TDATE; CASE CITIN: Boolean of True: (BirthPlace: String [40]); false: String [20]; entryport: string [20]; entrydate: tdate; exitdate: tdate); end; the Record Above is actually a single expression of two records that could describe a person: type TPersonCitizen = record FirstName, LastName: string [40]; BirthDate: tDate; BirthPlace: string [40] end; and type TPersonAlien = record FirstName, LastName: string [ 40]; birthdate: tdate; country: string [20]; entryport: string [20]; entrydate: tdate; eXi tDate: TDate; end; And as in a union, the combination of the two types of records makes for much more efficient programming, because a person could be expressed in a variety of ways Everything I explained above is pretty hypothetical stuff In Delphi.. , the TRect structure that describes a rectangle is actually a variant record: type TPoint = record X: Longint; Y: Longint; end; TRect = record case Integer of 0: (Left, Top, Right, Bottom: Integer); 1: (Topleft, Bottomright: TPOINT);