Loop through irregular enumeration in Delphi

前端 未结 6 831
逝去的感伤
逝去的感伤 2021-01-12 09:31

1) Does anyone know if it is possible to loop through an irregular enumeration in Delphi (XE)?

Looping over a normal enumeration is ok. From Delphi Basics:



        
6条回答
  •  不要未来只要你来
    2021-01-12 10:25

    A dirty option, useful for small enumerations:

    type
      TSuit = (Hearts = 1, Clubs, Diamonds = 10, Spades);
    var
      Suit: TSuit;
    begin
      for Suit in [Hearts, Clubs, Diamonds] do
        WriteLn(Ord(Suit));
    

    Works nice in Delphi 2007. Don't know about older versions. Be aware, using for Suit in [Hearts..Diamonds] do has the same problem as your loop.
    Btw, I use WriteLn() because I tested this in a console application. :-)

提交回复
热议问题