Search code examples
delphisetdelphi-2007

How to get the complementary values for a Delphi sets type variable?


Starting from a set type variable like TAnchors:

  TAnchorKind = (akLeft, akTop, akRight, akBottom);
  TAnchors = set of TAnchorKind;

I'm trying to get the complementary values.

var
  Tmp : TAnchors;
begin
  Tmp := [akLeft];
   ...
end;

I'm expecting to get all values of TAnchors which are not in the Tmp variable.

For example, starting from [akLeft], I'm expecting to get [akTop, akRight, akBottom].

I've tried using the not operator but it seems it doesn't work for Sets types.


Solution

  • The set operators are listed in the documentation. The not operator is not listed here which is why it cannot be used on a set. However, you are looking for the difference operator, -. Take the difference between the set including all members, and your set:

    [Low(TAnchorKind)..High(TAnchorKind)] - Anchors