我有这个代码
type TXSample = (xsType1,xsType2,xsType3,xsType4,xsType5,xsType6,xsTyp7,xsType8); // up to FXSample30; .. private FXSample = Set of TXSample; .. published property Sample: TXSample read FXSample write FXSample; .. //if Sample has a value of Sample := [xsType2,xsTyp7];
解决方法
如果您的集合永远不会超过32个可能性(Ord(High(TXSample))= 31),则将集合类型转换为整数并返回是非常好的:
type TXSamples = set of TXSample; var XSamples: TXSamples; begin ValueToStoreInDB := Integer(XSamples); Integer(XSamples) := ValueReadFromDB; end;
要更具体:SizeOf(TXSamples)必须与SizeOf(StorageTypeForDB)精确相等。因此,当将TXSamples类型转换为以下时,以下范围适用于Ord(High(TXSample)):
> Byte:Ord(High(TXSample))< 8> Word:8< = Ord(High(TXSample))< 16> Longword:16< = Ord(High(TXSample))< 32> UInt64:32< = Ord(High(TXSample))< 64