我有这个代码
type
TXSample = (xsType1, xsType2, xsType3, xsType4, xsType5, xsType6, 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, xsType4, xsType5, xsType6, xsTyp7];
如何保存/加载Sample的属性?
我想将其保存在数据库中。
可能吗?
最佳答案
如果您的集合不会超过32个可能性(Ord(High(TXSample)) <= 31
),那么将集合转换成Integer
并返回就可以了:
type
TXSamples = set of TXSample;
var
XSamples: TXSamples;
begin
ValueToStoreInDB := Integer(XSamples);
Integer(XSamples) := ValueReadFromDB;
end;
更具体地说:
SizeOf(TXSamples)
必须精确等于SizeOf(StorageTypeForDB)
。因此,在将Ord(High(TXSample))
类型转换为以下内容时,以下范围适用于TXSamples
:Byte: Ord(High(TXSample)) < 8
Word: 8 <= Ord(High(TXSample)) < 16
Longword: 16 <= Ord(High(TXSample)) < 32
UInt64: 32 <= Ord(High(TXSample)) < 64
关于delphi - 如何保存/加载类型集?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9553510/