TLama

Untitled

Jul 5th, 2014
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.81 KB | None | 0 0
  1. type
  2.   TCompatibilityKind = (
  3.     Delphi1,
  4.     Delphi2,
  5.     Delphi3
  6.   );
  7.   TCompatibility = set of TCompatibilityKind;
  8.  
  9.   TMyClass = class
  10.   private
  11.     FCompatibility: TCompatibility;
  12.     function GetCompatibility(Kind: TCompatibilityKind): Boolean;
  13.     procedure SetCompatibility(Kind: TCompatibilityKind; Value: Boolean);
  14.   public
  15.     // this property is for access to the set at once, if you need it; if you make it published, you will see
  16.     // it in the Object Inspector
  17.     property Compatibility: TCompatibility read FCompatibility write FCompatibility;
  18.     // and this one is for access to elementary values of the set; they are usually prefixed by "Is" or "Has";
  19.     // note, that this one you won't see in the Object Inspector if you make it published
  20.     property HasCompatibility[Kind: TCompatibilityKind]: Boolean read GetCompatibility write SetCompatibility;
  21.   end;
  22.  
  23. implementation
  24.  
  25. { TMyClass }
  26.  
  27. function TMyClass.GetCompatibility(Kind: TCompatibilityKind): Boolean;
  28. begin
  29.   Result := Kind in FCompatibility;
  30. end;
  31.  
  32. procedure TMyClass.SetCompatibility(Kind: TCompatibilityKind; Value: Boolean);
  33. begin
  34.   if Value then
  35.     Include(FCompatibility, Kind)
  36.   else
  37.     Exclude(FCompatibility, Kind);
  38. end;
  39.  
  40. // example usage
  41. procedure TForm1.Button1Click(Sender: TObject);
  42. var
  43.   MyClass: TMyClass;
  44. begin
  45.   MyClass := TMyClass.Create;
  46.   try
  47.     // write elementary property value
  48.     MyClass.HasCompatibility[Delphi1] := CheckBox1.Checked;
  49.     MyClass.HasCompatibility[Delphi2] := CheckBox2.Checked;
  50.     // read elementary property value
  51.     CheckBox3.Checked := MyClass.HasCompatibility[Delphi1];
  52.     CheckBox4.Checked := MyClass.HasCompatibility[Delphi2];
  53.  
  54.     // or you can access the MyClass.Compatibility set at once as you
  55.     // did before...
  56.   finally
  57.     MyClass.Free;
  58.   end;
  59. end;
Advertisement
Add Comment
Please, Sign In to add comment