如果您不需要更改delphi中預定義的實際變量,則可以實際執行其他操作。
所有你真正想要的是保存鍵 - 值對。
這在其他語言中被稱爲關聯數組/詞典/鍵值對
因此可以這樣說
Arr['color'] := 'red';
或者說
Arr[edit1.Text] := edit2.Text;
反向
然後
Caption := Arr['color'];
會將標題設置爲「紅色」或edit2.text中的任何內容,您可以根據需要添加任意數量的標題。
如果你有一個老版本的Delphi,你可以用Tlist類做同樣的事情,不要嵌套。
arr:= TStringList.Create;
arr.Add('color=red');
s := arr.Values['color'];
arr.free;
我需要嵌套太多,所以我用做在Delphi XE3字典,可以幫助您更多的未來過於關聯數組類。
unit assoc;
interface
uses System.Generics.Collections, System.SysUtils;
type TAssoc = class
private
fVal: Variant;
fStrict: Boolean;
fDict: TDictionary<Variant,TAssoc>;
function GetItem(Index: Variant): TAssoc;
procedure SetVal(v: Variant);
function GetVal:Variant;
public
/// <summary>
/// Returns the TAssocnode by default allowing you to chain Nodes
/// </summary>
/// <param name="Index">
/// Search index, can be any primitive type.
/// </param>
property Items[Index: Variant]: TAssoc read GetItem; default;
/// <summary>
/// Use this to read and write a node value.
/// </summary>
/// <value>
/// can be almost everything Try not to use objects in here.
/// </value>
property Val:Variant read GetVal write SetVal;
/// <summary>
/// Gives you a direct link to the tdictionary object alowing you to
/// itterate
/// </summary>
property All:TDictionary<Variant,TAssoc> read fDict;
/// <summary>
/// <para>
/// Usefull for creating keys in strict mode
/// </para>
/// <para>
/// Array.add('key');<br />Array['key'].Val = 1;
/// </para>
/// <para>
/// Wich would throw an error in strict mode as 'key' is not defined
/// <br />In non strict mode it will just be created for you
/// </para>
/// </summary>
function Add(Index: Variant):TAssoc; overload;
/// <summary>
/// <para>
/// Usefull for creating keys in strict mode and setting its value at the same time
/// </para>
/// <para>
/// Array.add('key',1);<br />Array['key'].Val = 1;
/// </para>
/// <para>
/// Wich would throw an error in strict mode as 'key' is not defined
/// <br />In non strict mode it will just be created for you
/// </para>
/// </summary>
function Add(Index: Variant; Value: Variant):TAssoc; overload;
/// <summary>
/// Strictmode will raise an exception when you try to:<br />- set a nod
/// that was already set instead of overwriting it.<br />- read a node
/// that was nto set
/// </summary>
/// <param name="strictRules">
/// Strictmode on of off
/// </param>
constructor Create(strictRules:Boolean);
destructor Free;
/// <summary>
/// Clears all underlying nodes
/// </summary>
procedure Clear;
end;
type TAssocEnum = TPair<Variant, TAssoc>;
implementation
{ TAssoc }
function TAssoc.Add(Index: Variant): TAssoc;
begin
Result := nil;
if(fDict<>nil) then begin // see if dict is or can be made
if(fDict.ContainsKey(Index)) then begin // see if the key is in there
if(fStrict = true) then begin // duplicate keys not strict
raise Exception.Create('Dictionary is in strict mode, the key "'+Index+'" was already set.');
end;
end else begin // dict made, just not the key
Result := TAssoc.Create(fStrict);
fDict.Add(Index,Result);
end;
end else begin // dict not found
begin // make dict and key
fDict := TDictionary<Variant,TAssoc>.Create(1);
Result := TAssoc.Create(fStrict);
fDict.Add(Index,Result);
end;
end;
end;
function TAssoc.Add(Index, Value: Variant): TAssoc;
begin
Result := Add(Index);
Result.Val := Value;
end;
procedure TAssoc.Clear;
var
Enum: TPair<Variant, TAssoc>;
begin
if(fDict<>nil) then begin
for Enum in fDict do begin
Enum.Value.Free;
end;
end;
fDict.Clear;
end;
constructor TAssoc.Create(strictRules:Boolean);
begin
fStrict := strictRules;
fDict := nil;
TVarData(fVal).VType := varEmpty;
end;
destructor TAssoc.Free;
var
Enum: TPair<Variant, TAssoc>;
begin
if(fDict<>nil) then begin
for Enum in fDict do begin
Enum.Value.Free;
end;
end;
end;
function TAssoc.GetItem(Index: Variant): TAssoc;
var
v: Variant;
begin
Result := nil;
if(fdict<>nil) then begin // see if dict is or can be made
if(fDict.ContainsKey(Index)) then begin // see if the key is in there
Result := fDict.Items[Index];
end else begin // dict made, just not the key
if (fStrict) then begin
raise Exception.Create('Dictionary is in strict mode, the key "'+Index+'" was not set.');
end else begin // if not set, create the index and make it into a assocnode
Result := TAssoc.Create(fStrict);
fDict.Add(Index,Result);
end;
end;
end else begin // if the key is not in there see if strict or not
if(fStrict) then begin // if strict then error index not set
raise Exception.Create('Dictionary is in strict mode, the key "'+Index+'" was not set.');
end else begin // if not set, create the index and make it into a assocnode
fDict := TDictionary<Variant,TAssoc>.Create(1);
Result := TAssoc.Create(fStrict);
fDict.Add(Index,Result);
end;
end;
end;
function TAssoc.GetVal: Variant;
begin Result := fVal; end;
procedure TAssoc.SetVal(v: Variant);
begin fVal := v; end;
// todo:
// make a kickass helper class
// output xml/json
// walk through tree
// parentnode property?
// make a delphi7 version using stringlist? (not worth it)
// have fun
end.
簡單的用法
Arr := TAssoc.Create(False);
Arr['color'].Val := 'red';
Arr[edit1.text].Val := edit2.text;
Arr.Free;
高級用法
Arr := TAssoc.Create(False);
Arr['cuzz']['cdcollection'][1]['name'].Val := 'Mika Album'; // set cd
Arr['cuzz']['cdcollection'][2]['name'].Val := 1; // set cd
Arr['cuzz']['cdcollection'][1]['name'].Val := 'Delphi 5'; // overwrite cd
Arr['cuzz']['cdcollection']['last']['name'].Val := 'Delphi xe3'; // overwrite cd
// list all cd's
for Enum in Arr['cuzz']['cdcollection'].All do begin
Memo1.Lines.Append('Property "'+String(Enum.Key)+'" holds "'+String(Enum.Value['name'].Val));
end;
Arr.Free;
德爾福石頭! 享受。
這可以通過擴展'RTTI'完成。但我認爲現在擴展的'RTTI'語法不在freepascal中(如果那是你的編譯器)。在自Delphi-2010以來包含的Delphi中。 – 2013-04-06 12:06:29
我正在使用Delphi!那麼你能舉出一個如何解決這個問題的例子嗎? – user2252031 2013-04-06 12:17:31
做一個rtti的網絡搜索 – 2013-04-06 12:27:26