Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Turbo Pascal - <b> variant variant records pp 82</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 VARIANT                       Variant Records                         pp 82


 Define:  A variant record allows for alternate fields and filed types
          within the same record, dependent upon a tag field value.

 Purpose: A single record can be used for multiple data structures by
          changing the value of the tag field.

 Notes:   A variant part consists of a tag field of a previously
          defined type, whose values determine the variant, followed by
          labels corresponding to each possible value of the tag field.

          Each label heads a field list which defines the type of
          the variant corresponding to the label.

          The fixed part of a record which contains the common fields
          must always preced the variant part.  A record can only have
          on variant part.  In a variant the parenthesis must be present
          even if they enclose nothing.

          The maintenance of the tag field values is the responsibility
          of the programmer, not the compiler.

          A record variant with a tag field is a discriminated union.
          The tag field identifier may be omitted altogether, leaving only
          the type identifier.  Such a record is known as a free union.

          The use of free unions is infrequent and should be practiced
          only by experienced programmers.


 ----------------------------------------------------------------------------


 Usage:
       TYPE
          NameType  = String [80]     ;  { 80 byte string type    }
          Origin    = (Citizen, Alien);  { Case constant type     }



       TYPE
          DateType  = Record
                      Day   : Byte    ;  { Byte day field         }
                      Month : Byte    ;  { Byte month field       }
                      Year  : Integer ;  { Integer year field     }
                      End             ;  { End of DateType record }

          PersonRec = Record
                      Name : NameType ;  { Common record field    }
                      Birth: DateType ;  { Common record field    }

                      (* Variant record part *)
                      Case CitizenShip : Origin Of
                           Citizen : (BirthPl   : NameType;
                                      BirthDate : DateType);
                           Alien   : (Country   : NameType;
                                      EntryDate : DateType;
                                      EntryPort : NameType);
                      End;               { Of Record = Person     }

       VAR
          CitizenShip : Origin;
          Person      : PersonRec;
          Date        : DateType ;

       BEGIN
          If CitizenShip = Alien then
             Begin
             With Person, Date Do
               Begin
               Country := 'France';
               Day     := 4;
               Month   := 11;
               Year    := 1950;
               End;
             End;

          If CitizenShip = Citizen then
             Begin
             With Person, Date Do
               Begin
               BirthPl := 'Boise ID';
               Day     := 4;
               Month   := 11;
               Year    := 1950;
               End;
             End;
       END.

See Also: Record With

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson