Delphi设置无效的Typecast

时间:2014-09-29 06:23:52

标签: delphi delphi-7

如何解决此无效的类型转换错误?当集合少于31个项目时,代码可以工作。以下是代码段:

type
  TOptionsSurveyTypes=(
    ostLoadSurvey00,
    ostLoadSurvey01,
    ostLoadSurvey02,
    ostLoadSurvey03,
    ostLoadSurvey04,
    ostLoadSurvey05,
    ostLoadSurvey06,
    ostLoadSurvey07,
    ostLoadSurvey08,
    ostLoadSurvey09,
    ostLoadSurvey10,
    ostEventLog01,
    ostEventLog02,
    ostEventLog03,
    ostEventLog04,
    ostEventLog05,
    ostSagSwell,
    ostTamper,
    ostWaveforms,
    ostDeviceList, 
    ostDeleteData,  
    ostTOUBillingTotal, 
    ostTOUPrevious,
    ostProfileGenericLoadSurvey01,
    ostProfileGenericLoadSurvey02,
    ostProfileGenericLoadSurvey03,
    ostProfileGenericLoadSurvey04,
    ostProfileGenericLoadSurvey05,
    ostProfileGenericLoadSurvey06,
    ostProfileGenericLoadSurvey07,
    ostProfileGenericLoadSurvey08,
    ostProfileGenericLoadSurvey09,
    ostProfileGenericLoadSurvey10,
    ostProfileGenericEventLog01,
    ostProfileGenericEventLog02,
    ostProfileGenericEventLog03,
    ostProfileGenericEventLog04,
    ostProfileGenericEventLog05,
    ostProfileGenericBillingTotal,
    ostProfileGenericPrevious,
    ostProfileGeneric
  );
TOptionsSurveyTypesSet=set of TOptionsSurveyTypes;

function TUserProcessRollback.SurveyRollBack:boolean;
var
  vRollbackDate: TDateTime;
  FOptions: LongWord;
begin
...
  if ostDeleteData in TOptionsSurveyTypesSet(FOptions) then   <-- invalid typecast error here
    vRollbackDate := 0.00
  else
    vRollbackDate := FRollbackDate;

...
end;

当我将集合减少到少于32个项目并且FOptions被声明为DWORD时,代码会编译。

由于

1 个答案:

答案 0 :(得分:2)

您的枚举类型有41个项目。每个字节保存8位。要拥有一组此枚举类型,至少需要41位。保持41位所需的最小字节数为6.因此,设置类型为6个字节。要确认这一点,您可以执行以下操作:

  ShowMessage ( inttostr ( sizeof ( TOptionsSurveyTypesSet ) ) );

DWORD是4个字节,因此不能将其类型转换为6个字节的类型。如果将fOptions声明为具有6个字节的类型,则代码将编译。

  FOptions: packed array [ 1 .. 6] of byte;

如果将枚举类型减少到32个或更少项,则set类型将为4个字节,因此DWORD中的类型转换将起作用。