[fpc-pascal] specify variable name with string variable
zh loza
zhloza at gmail.com
Sun Jul 7 22:33:36 CEST 2019
Hi all! It's my first answer to the mailing list, hopefully I won't
screw anything up.
On Sun, Jul 7, 2019 at 10:58 PM James Richters
<james at productionautomation.net> wrote:
>
> This might sound silly, but is it possible to somehow specify a variable with a string containing the name of the variable?
You might want to try using generic dictionaries. E.g. like this:
program Project1;
uses
SysUtils, Generics.Collections;
type
TVariableDictionary = specialize TDictionary<String, PWord>;
var
MyDictionary: TVariableDictionary;
MyVariable1: Word;
begin
MyDictionary := TVariableDictionary.Create;
MyDictionary.Add('MyVariable1', @MyVariable1);
MyDictionary['MyVariable1']^ := 100;
WriteLn(MyVariable1);
WriteLn(MyDictionary['MyVariable1']^);
FreeAndNil(MyDictionary);
end.
If you need to address variables of different type, try Variants (
https://wiki.freepascal.org/Variant ).
More information about the fpc-pascal
mailing list