2012-07-21 34 views
0

標題可能會在問題發佈後更新,但我以.ini文件開頭我希望將整數,字符串,布爾值保存到此.ini文件中。我可以用將多個數據類型讀入列表

WriteString 
WriteInteger 
WriteBool 

不那麼我想它讀入一個列表,其中當我從名單拉到數據會知道它的都準備好了一個整數或字符串或布爾?

目前我必須將所有東西都寫成字符串,然後讀入字符串列表。

+0

閱讀所有設置爲ReadString :) – Marcodor 2012-07-21 15:46:21

回答

2

如上所述,您可以將所有數據讀取爲字符串。你可以使用下面的函數來確定數據類型:

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; 
+0

那麼目前我都準備好知道,如果數據值我從拉StringList的是INT,字符串或布爾。我只想停止執行StrtoInt(來自stringlist的值)和StrToBool(來自stringlist的值)那麼我可以在stringlist中配置數據類型嗎?或任何列表類型。對不起,如果我沒有正確解釋第一次 – 2012-07-21 15:55:30

+0

更新的答案,包括數據類型。 – 2012-07-21 16:03:34

+0

要嘗試一下,從來沒有嘗試過這樣的事情。 – 2012-07-21 16:07:03