Delphi - 如何 Exclude/turn 关闭一组中的所有值?

Delphi - How to Exclude/turn off all values in a set?

Delphi XE6 - 我有一套。我想要一种关闭所有元素的简单方法。即,而不是 Exclude,类似于 ExcludeALL。我试图遍历所有元素,但出现错误。

代码

 type
      TSearchParametersType = 
       (smDUNSAvailable = 1, 
        smDUNSHit,    
        smDUNSMiss,    
        smDUNSAbsent, 
        smRegistryAvailable, 
        smRegistryHit, 
        smRegistryAbsent, 
        smRegistryMiss, 
        smNameAvailable, 
        smNameHitExact, 
        smNameHitWords, 
        smNameMiss  
    );

    // Now create a set type, where we can have a variable that has all the values of TSearchParametersType
    type
      TSearchParametersSet = set of TSearchParametersType;
 ...   

   var
   i : Integer;
   sSearchStatus: TSearchParametersSet;

    begin 
    for i :=  smDUNSAvailable to  smNameMiss do
      Exclude(sSearchStatus, i);

我得到的错误是"Incompatible Type: 'Integer' and TSearchParametersType. "

除了手动遍历每个元素之外,是否有一种简单的方法来排除所有元素?

谢谢

来自documentation

Every set type can hold the empty set, denoted by [].

因此您可以像这样将空集分配给您的变量:

sSearchStatus := [];

FWIW,您的代码失败,因为 smDUNSAvailablesmNameMiss 属于 TSearchParametersType 类型,因此与 [=15= 类型的变量 i 不兼容].为了使您的代码正常工作,您需要将循环变量更改为 TSearchParametersType.

类型

首先让我声明 David 的回答是正确的。

我将 post 另一个来展示如何手动完成。此代码可能会在其他时间派上用场:

var
  sSearchStatus: TSearchParametersSet;
  SearchParametersType : TSearchParametersType;        
begin 
  sSearchStatus := [smDUNSHit, smDUNSMiss, smDUNSAbsent, smRegistryAvailable, smRegistryHit];

  for SearchParametersType :=  low(TSearchParametersType) to  high(TSearchParametersType) do
    Exclude(sSearchStatus, SearchParametersType);
end;