0

我想动态加载并显示TVirtualStringTree来自一些不同 SQL 表的数据。这意味着每列的标题和内容每次都会包含不同类型的数据。

我的问题是如何优化内存使用、记录的定义和这种情况下的指针。

我的想法是这样做:

type
  TDataType = (dtUnknown, dtString, dtInteger, dtText, dtFloat, dtDateTime, dtDate, dtTime, dtBoolean);

  TData = record
    DataType: TDataType;
    AsString: String;
    AsInteger: Integer;
    AsText: TStrings;
    AsWord: Word;
    AsDateTime: TDateTime;
    AsDate: TDate;
    AsTime: TTime;
    AsBoolean: Boolean;
  end;
  TTreeData = array of TData;

  PTreeData= ^TTreeData;

实际上,记录中只有两个字段将包含数据:(DataType一直)和第二个字段,取决于DataType定义的(例如AsStringAsInteger)。初始化 Node 时,其他字段也会分配为内存吗?我也不喜欢DataType分配给每个节点的事实。必须有一种简单的方法来优化此记录。

请提出一些建议。

4

1 回答 1

0

当我需要做这样的事情时,我会在 TData 记录中使用 case 语句。

就像是

type
   TData = record
      case DataType: TDataType of
       dtUnknown:
         (AsUnknown: ???);
       dtString:
         (AsString: string);  // this is incorrect, string (and some others are not allowed)
       dtInteger:
         (AsInteger: Integer);

       ... and so on

         end;
      end;

http://docwiki.embarcadero.com/RADStudio/Seattle/en/Structured_Types#Variant_Parts_in_Records

看看 TVarRec。Delphi 在 Format 语句中实现 'array of const' 变量 ans 时使用开放数组 TVarRec。

您可能只需要 VirtualStringTree 中的两列。一个用于类型,另一个用于值(表示为字符串)

于 2016-04-17T05:08:10.510 回答