2014-09-29 81 views
0

如何修复这个无效的类型转换错误?该代码在集合少于31个项目时起作用。下面的代码片段:Delphi设置无效的类型转换

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,代码编译。

谢谢

回答

2

您的枚举类型有41个项目。每个字节保存8位。有一组枚举类型需要至少41位。保存41位所需的最小字节数是6.所以设置类型是6个字节。为了证实这一点,您可以执行此:

ShowMessage (inttostr (sizeof (TOptionsSurveyTypesSet))); 

一个DWORD是4个字节,所以它不能被强制转换成一个类型,它是6个字节。如果你声明fOptions是一个6字节的类型,你的代码将被编译。

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

如果减少枚举类型为32个或更少的项目,然后该组类型将是4个字节,因此从DWORD类型转换将工作。

+0

感谢您的解释。然而,即使我将FOptions改为Longword,它仍然无法编译? – user474079 2014-10-10 09:31:02

+0

DWORD只是Longword的别名。为了使这种类型转换编译这两种类型必须具有相同的字节数。 DWORD和Longword有4个字节。你的SET类型有6个字节。 – 2014-10-11 02:24:55

相关问题