Thursday, February 17, 2011

How do I fill a Delphi set?

If I have a type defined as a set of an enumerated type, it's easy to create an empty set with [], but how do I create a full set?

EDIT: Yeah, the obvious solution is to use a for loop. That's also a really bad solution if there's another way. Does anyone know of a way that'll work in constant time?

From stackoverflow
  • Per Barry's suggestion:

    FillChar(VarSet, SizeOf(VarSet), $FF);
    
  • Low() and High() are "compiler magic" functions that can be evaluated at compile time. This allows their use in constant declarations like the following:

    var
      MySet : TBorderIcons;
      MySet2 : TBorderIcons;
    const
      AllIcons : TBorderIcons = [Low(TBorderIcon)..High(TBorderIcon)];
    begin
      MySet := [Low(TBorderIcon)..High(TBorderIcon)];
      MySet2 := AllIcons;
    end;
    
    F.D.Castel : Dude! That was GOOD! ;) Thank you!
    F.D.Castel : I'm even deleting my own answer, after that ;)

0 comments:

Post a Comment