【问题标题】:Read in multiple data types to a list将多种数据类型读入列表
【发布时间】:2012-07-21 15:36:15
【问题描述】:

一旦发布问题,标题可能会更新,但我从一个 .ini 文件开始,我想将 Integers 、 Strings 、 Bool 保存到这个 .ini 文件中。我可以用

WriteString
WriteInteger
WriteBool

然后我想将它读入一个列表,当我从列表中提取数据时它会知道它已经准备好一个整数或字符串或布尔值?

目前我必须将所有内容都写成字符串,然后读入字符串列表。

【问题讨论】:

  • 将所有设置读取为 ReadString :)

标签: delphi delphi-xe2 ini


【解决方案1】:

如前所述,您可以将所有数据读取为字符串。您可以使用以下函数来确定数据类型:

type
  TDataType = (dtString, dtBoolean, dtInteger);

function GetDatatype(const AValue: string): TDataType;
var
  temp : Integer;
begin
  if TryStrToInt(AValue, temp) then
    Result := dtInteger   
  else if (Uppercase(AValue) = 'TRUE') or (Uppercase(AValue) = 'FALSE') then
    Result := dtBoolean
  else
    Result := dtString;
end;


You can (ab)use the object property of the stringlist to store the datatype:


procedure TMyObject.AddInteger(const AValue: Integer);
begin
  List.AddObject(IntToStr(AValue), TObject(dtInteger));
end;

procedure TMyObject.AddBoolean(const AValue: Boolean);
begin
  List.AddObject(BoolToStr(AValue), TObject(dtBoolean));
end;

procedure TMyObject.AddString(const AValue: String);
begin
  List.AddObject(AValue, TObject(dtString));

end; 

function TMyObject.GetDataType(const AIndex: Integer): TDataType;
begin
  Result := TDataType(List.Objects[AIndex]);
end;

【讨论】:

  • 好吧,目前我已经准备好知道我从字符串列表中提取的数据值是 int、string 还是 bool。我只想停止执行 StrtoInt(value from stringlist) 和 StrToBool(value from stringlist) 那么我可以在 stringlist 中分配数据类型吗?或任何列表类型。对不起,如果我第一次没有正确解释
  • 更新了答案以包含数据类型。
  • 阅读有关隐式自动转换的 Variant 和 TValue 等类型
猜你喜欢
  • 2011-05-19
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 2020-02-16
  • 2021-06-16
  • 1970-01-01
  • 2016-05-13
  • 2012-01-01
相关资源
最近更新 更多