Forum > General

Sets - shorter expression

(1/4) > >>

Blaazen:
Hello,

do I have any lack of Pascal languge? Is this possible to write in one line?
I mean some type-saving way to control one item of set with boolean.

--- Code: ---type
  TOption = (oOne, oTwo, oThree);
  TOptions = set of Option;
...
if (Sender as TCheckBox).Checked
    then Options:=Options+[oThree]
    else Options:=Options-[oThree];     

--- End code ---
Thanks.

typo:
Do you refer to the set operators? They are OK.

bigeno:
Do you mean something in this way ?
--- Code: ---  Options[oThree]:=(Sender as TCheckBox).Checked;

--- End code ---
if yes, I think this is not possible.

taazz:
personally I have an ever growing set of iif functions to use in such cases eg

--- Code: ---function IIF(Check :Boolean; TrueResult, FalseResult :Integer):Integer;overload;inline;
function IIF(Check :Boolean; TrueResult, FalseResult :String):String;overload;inline;
....
implementation
function IIF(Check :Boolean; TrueResult, FalseResult :String):String;overload;inline;
begin
  If Check then Result := TrueResult else Result := FalseResult;
end;
......

--- End code ---

but in this case I would do something like this.

--- Code: ---function IIF(const Check :Boolean; const aSet:TOptions; const aOpt :TOption):TOptions;overload;inline;
begin
  Result := aSet;
  if Check then Include(Result, aOpt) else Exclude(Resut, aOpt)
end;

--- End code ---
And its use looks like
--- Code: --- Options := iif((Sender as TCheckBox).Checked,Options,oThree);
--- End code ---

Blaazen:
Thanks. In past I used function too, I named it SwitchOption instead of IIF. I hoped there exists some trick with set operators. It seems that Mr. Wirth omitted this.  :-[

Navigation

[0] Message Index

[#] Next page

Go to full version