集合类型是否适合循环迭代?

时间:2015-10-08 17:31:00

标签: delphi enums set delphi-6

有以下枚举:

TDays = (tdSunday, tdMonday, tdTuesday, tdWednesday, tdThursday, tdFriday, tdSaturday);

以下一组枚举:

TChoosenDays = set of TDays;

现在,定义以下数组:

var
  ArrayStringDaysAcronym : array [TDays] of String = ('SD', 'MD', 'TU', 'WE', 'TH', 'FR', 'ST');

假设我有一个声明为:

的变量
var
  Foo: TChoosenDays;
begin
  Foo:= [tdSunday, tdMonday, tdTuesday];

如何迭代集合中的所有成员?

注意:以下不编译,我知道我可以使用数组而不是“set of”,这是唯一的方法吗?

function ConcatAcronyms: String;
var
  Item: TDays; 
begin
  Result:= '';
  for Item:= Low(Foo) to High(Foo) do
  begin
    Result:= Result + '; '+ ArrayStringDaysAcronym[Item];
  end;
end;     

1 个答案:

答案 0 :(得分:3)

是。由于你正在使用D6,你可以这样做:

function ConcatAcronyms: String;
var
  Item: TDays;
begin
  Result:= '';
  for Item:= Low(TDays) to High(TDays) do
  begin
    if Item in Foo then begin
      if Result <> '' then
        Result := Result + ';'  // assuming you don't want Result to start with  a ';'
      Result:= Result + ArrayStringDaysAcronym[Item];
    end;
  end;
end;