gpt4 book ai didi

delphi - 更改名称作为输入给出的变量 (Pascal)

转载 作者:行者123 更新时间:2023-12-03 15:42:10 24 4
gpt4 key购买 nike

我想知道是否有基本的方法来解决这个问题。对 Pascal 的特定解决方案也特别感兴趣。

假设我们有一个包含 2 个输入的表单:“InputVARIABLE”和“InputVALUE”。

在输入变量区域中,用户键入他想要更改的变量的名称。在“InputVALUE”区域中,用户键入该变量的值。

因此,对于输入文本“颜色”和“蓝色”:如何在不使用 IF 语句的情况下更改变量 VAR_color,例如:

If InputVARIABLE.Text = 'color' Then VAR_color := InputVALUE.Text;

是否有一些函数,例如:

ChangeVariable(InputVARIABLE.Text, InputValue.Text)

希望能帮到你!

最佳答案

如果您不需要更改 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;

德尔福太棒了!享受吧。

关于delphi - 更改名称作为输入给出的变量 (Pascal),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15850406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com