Even though there is a gambiarra (which I highly doubt) that serializes local identifiers on String
, the proposal below is simpler and (in my opinion) more elegant.
interface
uses Vcl.Dialogs, Classes;
type
TMyArray = array[0..4] of String;
TStringHash = class
private
FKeys : TStringList;
FValues : Array of TMyArray;
function GetContent(Key: String; Index : Integer): String;
procedure SetContent(Key : String; Index : Integer; Value: String);
public
property Contents[Key : String; Index : Integer] : String read GetContent write SetContent; Default;
constructor Create;
function Count : Integer;
end;
implementation
constructor TStringHash.Create;
begin
inherited;
FKeys := TStringList.Create;
FKeys.Add('');
SetLength(FValues, 1);
FValues[0][0] := '';
end;
function TStringHash.Count: Integer;
begin
Result := FKeys.Count;
end;
function TStringHash.GetContent(Key: String; Index : Integer) : String;
var
I : Integer;
begin
Result := '';
I := FKeys.IndexOf(Key) ;
if I > -1 then
Result := FValues[I][Index];
end;
procedure TStringHash.SetContent(Key : String; Index : Integer; Value: String);
var
I : Integer;
begin
I := FKeys.IndexOf(Key) ;
if I > -1 then
FValues[I][Index] := Value
else
begin
SetLength(FValues, Length(FValues) + 1);
FValues[Length(FValues) - 1][Index] := Value;
FKeys.Add(Key);
end;
end;
For the use just instantiate TStringHash
with the line to follow MinhaHash := TStringHash.Create;
.
And to manipulate values use: Valor := MinhaHash['NomeVar', 0];
and MinhaHash['NomeVar', 0] := Valor;
.
Example
Create a Test.dpr. file Double-click (assuming you have Delphi installed) and after Delphi opens the file paste the (contents) and Compile (F9).
I’m pretty sure there’s a less complicated way to do what you’re willing to do.
– EProgrammerNotFound
The only way I have in mind at the moment, but not ideal is by making a case and associating each received value.
– Corvo
The intention would be to "convert" this value of String, recognizing it as the declared variable.
– Corvo
But what’s the point?
– EProgrammerNotFound
These arrays will always be the same size?
– EProgrammerNotFound
The goal is to recognize the value as a variable, to be clearer I have a project of the technician and at the moment I am focusing only on a structure of the program. If possible to do this without using case would already help.
– Corvo
Correct, but different names and values
– Corvo
What version of Delphi? Where are these variables declared?
– EProgrammerNotFound
I’m using Delphi XE6 (mobile development), but I think it does not influence in a certain way the syntax applied in Pascal, after all you have a solution?
– Corvo
Influence if you make a solution using RTTI
– EProgrammerNotFound
Let’s go continue this discussão in chat.
– Corvo
Why not use an array of array [1.. 4] of string?
– EProgrammerNotFound
What would be the application of this resource?
– Caputo
Use a
Hash
, mapping from a variable name to thearray
correspondent.– Vinícius Gobbo A. de Oliveira