Identifying a String as Pascal variable name

Asked

Viewed 884 times

0

I have 50 variable array type and I have a method that will receive a String containing a name of an existing variable. I would like to know what is the ideal way to identify the value of the received string as one of the 50 variables declared in the program.

Here is one of the 50 variable names:

var
x:array[1..4] of String;

Here is the method:

procedure IdentificarVariavel(s:String); 
begin
     //
     // tratar o valor da String s recebida como váriavel x;
     //
end;

I need to treat the value of String s as a variable. OBS: would not like to create a case.

  • I’m pretty sure there’s a less complicated way to do what you’re willing to do.

  • The only way I have in mind at the moment, but not ideal is by making a case and associating each received value.

  • The intention would be to "convert" this value of String, recognizing it as the declared variable.

  • But what’s the point?

  • These arrays will always be the same size?

  • 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.

  • Correct, but different names and values

  • What version of Delphi? Where are these variables declared?

  • 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?

  • Influence if you make a solution using RTTI

  • 1

    Why not use an array of array [1.. 4] of string?

  • What would be the application of this resource?

  • 1

    Use a Hash, mapping from a variable name to the array correspondent.

Show 9 more comments

1 answer

2


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).

  • Murilo, if the answer did not clarify the doubt, comment when you can, please.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.