delphitypes

What Delphi type for 'set of integer'?


I have several hardcoded validations like these:

const
  cLstAct      =  1;   
  cLstOrg      =  4;
  cLstClockAct = 11;
const
  FUNCT_1 = 224;
  FUNCT_2 = 127;
  FUNCT_3 =   3;

if lFuncID in [FUNCT_1,FUNCT_2,FUNCT_3] then ...
if not (lListType in [cLstAct..cLstOrg,cLstClockAct]) then ...
if not (lPurpose in [0..2]) then ...

that I want to replace with a common method like

function ValidateInSet(AIntValue: integer; AIntSet: @@@): Boolean;
begin
  Result := (AIntValue in AIntSet);
  if not Result then ...
end;  

but what type to choose for AIntSet?

Currently the values to be tested throughout the code go up to a const value 232 (so I can e.g. use a TByteSet = Set of Byte), but I can foresee that we will bump into the E1012 Constant expression violates subrange bounds when the constant values exceed 255.

My Google-fu fails me here...

(Currently on Delphi Seattle Update 1)


Solution

  • You can use an array of Integer:

    function ValidateInSet(AIntValue: integer; AIntSet: array of Integer): Boolean;
    var
      I: Integer;
    begin
      Result := False;
      for I := Low(AIntSet) to High(AIntSet) do
      begin
        if AIntSet[I] = AIntValue then
        begin
          Result := True;
          Break;
        end;
      end;
      if not Result then ...
    end;  
    

    const
      cLstAct      =  1;   
      cLstOrg      =  4;
      cLstClockAct = 11;
    const
      FUNCT_1 = 224;
      FUNCT_2 = 127;
      FUNCT_3 =   3;
    
    if ValidateInSet(lFuncID, [FUNCT_1, FUNCT_2, FUNCT_3]) then ...
    if not ValidateInSet(lListType, [cLstAct, 2, 3, cLstOrg, cLstClockAct]) then ...
    if not ValidateInSet(lPurpose, [0, 1, 2]) then ...