2014-09-29 82 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

相關問題