[fpc-pascal] Case in Record

Henry Vermaak henry.vermaak at gmail.com
Tue Oct 6 18:04:09 CEST 2009


2009/10/6 章宏九 <secludedsage at gmail.com>:
> Thank you. I saw all the examples you gave, but I still cannot master
> how to use a variant record without a specified tag.
>
> For example:
> type
>  TShapeList = (Rectangle, Triangle, Circle, Ellipse, Other);
>  TFigure = record
>    case TShapeList of
>      Rectangle: (Height, Width: Real);
>      Triangle: (Side1, Side2, Angle: Real);
>      Circle: (Radius: Real);
>      Ellipse, Other: ();
>  end;
> var
>  Figure1: TFigure;
>
> How can I specified the TShapelist value in a Figure1?

You can specify an identifier in the case statement, which will become
a new field.  You can then specify what the record is used for
currently, e.g.:

TShapeList = (Rectangle, Triangle, Circle, Ellipse, Other);
TFigure = record
  case Shape: TShapeList of
          ^^^^^^^
    Rectangle: (Height, Width: Real);
    Triangle: (Side1, Side2, Angle: Real);
    Circle: (Radius: Real);
    Ellipse, Other: ();
  end;

Then you can set .Shape to Rectangle, for instance, and fill in
.Height and .Width.  A function receiving a variable of this record
can then see what shape it is currently used for.  You need to
maintain this .Shape field, though.

Henry



More information about the fpc-pascal mailing list