[fpc-pascal] specify variable name with string variable
svaa at ciberpiula.net
Mon Jul 8 21:40:20 CEST 2019
El 07/07/2019 a las 21:58, James Richters escribió:
> This might sound silly, but is it possible to somehow specify a variable with a string containing the name of the variable?
> For example:
> MyVariable1 : Word;
> MyVariableName : String;
> Procedure ShowVariable(Variablename);
> Writeln(Variablename,' = $', inttohex( (Somehow get the value of the variable specified by the name here ) ,4));
> MyVariableName:= 'MyVariable1';
> Is such a thing possible?
What is it for?
procedure ShowVariable(const Variablename:string; const value:word);
Writeln(Variablename,' = $'+inttohex(value ,4));
And as far as I understand, you want to call
ShowVariable('myVar',myVar), but not wasting time writing the name
enclosed in quotes, let the program get the identifier name in runtime,
or the other way around, let the program get the value from a name
passed in runtime.
Bad luck, Pascal is not a script language, it is not aware of identifier
names in runtime there is no "eval". Thanks to that it gets rids of all
the burden of having to keep in runtime structures to know the name,
type and value of variable. It generate fast native programs.
Nevertheless, there are ways, you can program that in Freepascal using
classes, properties, RTII etc. It is more flexible and powerful. But
obviously it has a price in complexity. Instead or using the standard
operators and reference a simple variable, you must use a more
As far as I see, you don't intend to let the program create new vars in
runtime, you just want to write the identifiers of the vars you have
hardcoded. Is it worth?
(I hope it is not just for debugging)
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the fpc-pascal