本文介绍了如何保存/加载类型集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码

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的属性?

i想保存在数据库中。

是可能的吗?

how can i save/load the property of Sample?
i would like to save it in the database.
is it possible?

推荐答案

如果您的设置永远不会超过32种可能性( Ord(High(TXSample))),那么将集合类型转换为 Integer 并返回:

Provided your set will never exceed 32 possibilities (Ord(High(TXSample)) <= 31), then it is perfectly fine to typecast the set into an Integer and back:

type
  TXSamples = set of TXSample;
var 
  XSamples: TXSamples;
begin
  ValueToStoreInDB := Integer(XSamples);
  Integer(XSamples) := ValueReadFromDB;
end;

更具体地说: SizeOf(TXSamples)必须等于 SizeOf(StorageTypeForDB)。因此,当将 TXSamples 转换为:

To be more specific: SizeOf(TXSamples) has to be precisely equal to SizeOf(StorageTypeForDB). Thus the following ranges apply for Ord(High(TXSample)) when typecasting TXSamples to:


  • 字节:Ord(高(TXSample))< 8

  • Word:8< = Ord(High(TXSample))< 16

  • 长字:16

  • UInt64:32< = Ord(High(TXSample))< 64

  • Byte: Ord(High(TXSample)) < 8
  • Word: 8 <= Ord(High(TXSample)) < 16
  • Longword: 16 <= Ord(High(TXSample)) < 32
  • UInt64: 32 <= Ord(High(TXSample)) < 64

这篇关于如何保存/加载类型集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 14:58