[fpc-pascal] evaluation of set constant

Howard Page-Clark hdpc at talktalk.net
Wed Dec 22 15:17:39 CET 2010


On 22/12/10 1:15, Torsten Bonde Christiansen wrote:
> type
>    TMyType = (a, b, c ,d);
>    TMyTypes = set of TMyTypes;
>
> const
>    SetX:        TMyTypes = (a, b);
>    SetY:        TMyTypes = (c, d);
>    SetCombined: TMyTypes = SetX + SetY;   // this gives me an "Error: Illegal expression"

Because you have not declared your sets with the correct square brackets.
Try this sample:

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
   Classes, Forms, Controls, Dialogs, StdCtrls;

type
   TForm1 = class(TForm)
     Button1: TButton;
     procedure Button1Click(Sender: TObject);
   end;

   TMyType = (a, b, c , d);
   TMyTypes = set of TMyType;

const
   SetX:      TMyTypes = [a, b];
   SetY:      TMyTypes = [c, d];
   typeNames: array [TMyType] of string = ('a', 'b', 'c', 'd');

var
   Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
var SetCombined: TMyTypes;
     myType: TMyType;
     s: string;
begin
   s := '';
   setCombined :=  SetY + SetX;
   for myType in setCombined do
     s := s + typenames[myType];
   ShowMessage(s);
end;

end.

HTH

Howard



More information about the fpc-pascal mailing list