Forum > General

Sets - shorter expression

<< < (2/4) > >>

Mike.Cornflake:
I see no practical use for the following code, and would normally use your form of code, however - the challenge was raised :-) 
More set up time, but only one line of code.  In your case of course you would use (Sender as TCheckBox).Checked instead of bFlag...


--- Code: ---Procedure TForm1.Button1Click(Sender: TObject);
Type
  TOption = (oOne, oTwo, oThree);
  TOptions = set of TOption;
Var
  Options: TOptions;
  bFlag: Boolean;
Const
  LU_Option: Array[Boolean] of TOptions = ([], [oThree]);
Begin
  Options := [oOne, oTwo, oThree];
  bFlag := False;

  Options := Options - [oThree] + LU_Option[bFlag];
End; 
--- End code ---
LU is shorthand for Look Up
I don't think Mr Wirth did too bad :-)

bigeno:
Nice  8)

Blaazen:
I observed that other Pascal compiler (Delphi, GNU Pascal) cannot do it too. And it's not possible to hack it via Variant.

BeniBela:

--- Code: ---PByte(@Options + ord(oThree) div 8)^ := (PByte(@Options + ord(oThree) div 8)^ or (1 shl ord(oThree))) and ((ord(bFlag)*$FF or not (1 shl ord(oThree)))) ;
--- End code ---

Mike.Cornflake:
:-)  Brilliant.  I've absolutely no clue how that code works, but at least you can easily change the option you're changing which is a distinct advantage over mine :-)

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version